| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
 大量收QQ微信精准粉/交友粉，非诚勿扰 千里之行 始于足下

得分:0

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;
}
得分:0

#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--;
}
if(num[i]==num[i+1]) while(i<len&&num[i]==num[i+1]) i++;
}
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;
}

• 12
• 2/2页
• 1
• 2

Powered by Discuz, Processed in 0.029249 second(s), 9 queries.