11
代码语言:javascript复制#include <stdio.h>
#include <iostream>
#include <vector>
//#include <map>
#if(__cplusplus == 201103L)
#include <unordered_map>
#include <unordered_set>
#else
#include <tr1/unordered_map>
#include <tr1/unordered_set>
namespace std
{
using std::tr1::unordered_map;
using std::tr1::unordered_set;
//using std::tr1;
//using std::tr1;
}
//#include<tr1/unordered_map>
//using namespace std::tr1;
#endif
//#include <unordered_map>
using namespace std;
//using namespace std::tr1;
vector<vector<int> > threeSum(vector<int>&);
int main(void) {
cout<<"ans"<<endl;
int out[6]={-1,0,1,5,3,4};
vector<int> nums(out,out 6);
nums.push_back(-1);
nums.push_back(0);
nums.push_back(1);
nums.push_back(2);
nums.push_back(5);
nums.push_back(6);
cout<<nums.size()<<endl;
for(int i=0;i<nums.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
cout<<nums[i];
}
vector<vector<int> > ans;
ans=threeSum(nums);
//cout<<"ans"<<endl;
//cout<<ans[0][0]<<"ans"<<endl;
//cout<<ans[0][1]<<endl;
//cout<<ans[0][2]<<endl;
//cout<<"ans"<<endl;
vector<vector<int> >::iterator tmp;
for(int i=0;i<ans.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
//cout<<ans[i][0];
}
//cout<<ans[0][0]<<"ans"<<endl;
//cout<<ans[0][1]<<endl;
//cout<<ans[0][2]<<endl;
//for (tmp = ans.begin(); ans != nums.end(); tmp)
// {
// cout << "(" << tmp->first << ", " << tmp->second << ")" << " ";
// }
return 0;
}
unordered_map<int ,int> mm;
//vector<vector<int> > ans;
vector<vector<int> > threeSum(vector<int>& nums) {
//::sort(nums.begin(),nums.end());
//vector<int>::iterator tmp;
//for (tmp = nums.begin(); tmp != nums.end(); tmp)
// {
// cout << "(" << tmp->first << ", " << tmp->second << ")" << " ";
// }
vector<vector<int> > ans;
for(int i=0;i<nums.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
}
int preI=100000 10;
int preJ=100000 10;
for(int i=0;i<nums.size();i ){
if(nums[i]==preI)continue;
mm[nums[i]]--;
for(int j=i 1;j<nums.size();j ){
int find=0-nums[i]-nums[j];
if(find>=nums[i]&&find>=nums[j]){
if(mm[find]>0){
//ans.push_back(vector<int>{nums[i],nums[j],find});
vector<int> v;
v.push_back(nums[i]);
v.push_back(nums[j]);
v.push_back(find);
ans.push_back(v);
}
}
mm[nums[j]] ;
preJ=nums[j];
}
mm[nums[i]] ;
preJ=nums[i];
}
return ans;
}
22
代码语言:javascript复制#include <stdio.h>
#include <iostream>
#include <vector>
//#include <map>
#if(__cplusplus == 201103L)
#include <unordered_map>
#include <unordered_set>
#else
#include <tr1/unordered_map>
#include <tr1/unordered_set>
namespace std
{
using std::tr1::unordered_map;
using std::tr1::unordered_set;
//using std::tr1;
//using std::tr1;
}
//#include<tr1/unordered_map>
//using namespace std::tr1;
#endif
//#include <unordered_map>
using namespace std;
//using namespace std::tr1;
vector<vector<int> > threeSum(vector<int>&);
int main(void) {
cout<<"ans"<<endl;
int out[6]={-1,0,1,5,3,4};
//vector<int> nums(out,out 6);
vector<int> nums;
nums.push_back(-1);
nums.push_back(0);
nums.push_back(1);
nums.push_back(2);
nums.push_back(5);
nums.push_back(6);
cout<<nums.size()<<endl;
for(int i=0;i<nums.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
cout<<nums[i];
}
vector<vector<int> > ans;
ans=threeSum(nums);
//cout<<"ans"<<endl;
//cout<<ans[0][0]<<"ans"<<endl;
//cout<<ans[0][1]<<endl;
//cout<<ans[0][2]<<endl;
//cout<<"ans"<<endl;
vector<vector<int> >::iterator tmp;
for(int i=0;i<ans.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
//cout<<ans[i][0];
}
//cout<<ans[0][0]<<"ans"<<endl;
//cout<<ans[0][1]<<endl;
//cout<<ans[0][2]<<endl;
//for (tmp = ans.begin(); ans != nums.end(); tmp)
// {
// cout << "(" << tmp->first << ", " << tmp->second << ")" << " ";
// }
return 0;
}
unordered_map<int ,int> mm;
//vector<vector<int> > ans;
vector<vector<int> > threeSum(vector<int>& nums) {
//::sort(nums.begin(),nums.end());
//vector<int>::iterator tmp;
//for (tmp = nums.begin(); tmp != nums.end(); tmp)
// {
// cout << "(" << tmp->first << ", " << tmp->second << ")" << " ";
// }
vector<vector<int> > ans;
for(int i=0;i<nums.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
}
int preI=100000 10;
int preJ=100000 10;
for(int i=0;i<nums.size();i ){
if(nums[i]==preI)continue;
mm[nums[i]]--;
for(int j=i 1;j<nums.size();j ){
int find=0-nums[i]-nums[j];
if(find>=nums[i]&&find>=nums[j]){
if(mm[find]>0){
//ans.push_back(vector<int>{nums[i],nums[j],find});
vector<int> v;
v.push_back(nums[i]);
v.push_back(nums[j]);
v.push_back(find);
ans.push_back(v);
}
}
mm[nums[j]] ;
preJ=nums[j];
}
mm[nums[i]] ;
preJ=nums[i];
}
return ans;
}
ans的函数返回值总是不对,
代码语言:javascript复制ans没有值,是怎么回事???
vector<vector<int> > ans=threeSum(nums);
下面pushback是有值的,但是这方法也太笨了
代码语言:javascript复制#include <stdio.h>
#include <iostream>
#include <vector>
//#include <map>
#if(__cplusplus == 201103L)
#include <unordered_map>
#include <unordered_set>
#else
#include <tr1/unordered_map>
#include <tr1/unordered_set>
namespace std
{
using std::tr1::unordered_map;
using std::tr1::unordered_set;
//using std::tr1;
//using std::tr1;
}
//#include<tr1/unordered_map>
//using namespace std::tr1;
#endif
//#include <unordered_map>
using namespace std;
//using namespace std::tr1;
vector<vector<int> > threeSum(vector<int>&);
vector<vector<int> > ans;
int main(void) {
cout<<"ans"<<endl;
int out[6]={-1,0,1,5,3,4};
//vector<int> nums(out,out 6);
vector<int> nums;
nums.push_back(-1);
nums.push_back(0);
nums.push_back(1);
nums.push_back(2);
nums.push_back(5);
nums.push_back(6);
cout<<nums.size()<<endl;
for(int i=0;i<nums.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
cout<<nums[i];
}
vector<vector<int> > ans=threeSum(nums);
for(int i=0;i<threeSum(nums).size();i ){
ans.push_back(threeSum(nums)[i]);
}
//ans=threeSum(nums);
//cout<<"ans"<<endl;
//cout<<ans[0][0]<<"ans"<<endl;
//cout<<ans[0][1]<<endl;
//cout<<ans[0][2]<<endl;
//cout<<"ans"<<endl;
vector<vector<int> >::iterator tmp;
for(int i=0;i<ans.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
//cout<<ans[i][0];
}
cout<<endl<<ans.size()<<"ans"<<endl;
cout<<endl<<threeSum(nums).size()<<"ans"<<endl;
//cout<<ans[0][0]<<"ans"<<endl;
//cout<<ans[0][1]<<endl;
//cout<<ans[0][2]<<endl;
//for (tmp = ans.begin(); ans != nums.end(); tmp)
// {
// cout << "(" << tmp->first << ", " << tmp->second << ")" << " ";
// }
return 0;
}
unordered_map<int ,int> mm;
//vector<vector<int> > ans;
vector<vector<int> > threeSum(vector<int>& nums) {
//::sort(nums.begin(),nums.end());
//vector<int>::iterator tmp;
//for (tmp = nums.begin(); tmp != nums.end(); tmp)
// {
// cout << "(" << tmp->first << ", " << tmp->second << ")" << " ";
// }
vector<vector<int> > ans;
for(int i=0;i<nums.size();i ){
//mm(nums[i])=mm(nums[i]) nums[i];
}
int preI=100000 10;
int preJ=100000 10;
for(int i=0;i<nums.size();i ){
if(nums[i]==preI)continue;
mm[nums[i]]--;
for(int j=i 1;j<nums.size();j ){
int find=0-nums[i]-nums[j];
if(find>=nums[i]&&find>=nums[j]){
if(mm[find]>0){
//ans.push_back(vector<int>{nums[i],nums[j],find});
vector<int> v;
v.push_back(nums[i]);
v.push_back(nums[j]);
v.push_back(find);
ans.push_back(v);
}
}
mm[nums[j]] ;
preJ=nums[j];
}
mm[nums[i]] ;
preJ=nums[i];
}
return ans;
}