回复 5楼 rjsp
vector<vector<int> >是指二维数组对吧,我不明白为什么要用到二维数组...我把二维数组换成一维的就不会提示有错了.但还是出不了结果...
程序代码:
#include<iostream> #include<vector> using namespace std; vector<int> threeSum(vector<int> &num) { vector<int> result; int len=num.size(); for(int i=0;i<len;++i) { int target=0-num[i]; int start=i+1,end=len-1; while(start<end) { if(num[start]+num[end]==target) { result.push_back(num[i]); result.push_back(num[start]); result.push_back(num[end]); start++;end--; while(start<end&&num[start]==num[start-1]) { start++; } while((start<end) && (num[end]==num[end+1])) { end--; } } else if(num[start]+num[end]<target) start++; else end--; } while(i<len) if(num[i]==num[i+1]) i++; } for(vector<int>::iterator it=result.begin ();it!=result.end ();it++) cout<<it<<' '; cout<<endl; return result; } void main(){ int a[8]={-9,-3,-1,0,1,2,3,4}; vector<int> num(a,a+8); vector<int> b(threeSum(num)); for(vector<int>::iterator it=b.begin();it!=b.end();++it) cout<<*it<<" "; cout<<endl; }