mercredi 25 février 2015

find all unique triplets in array with sum equal to zero in c++

Hello I have refered this program and there mention in comment // DO NOT write int main() function, and when i run run this code it will give me error:- /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function _start': (.text+0x20): undefined reference tomain' collect2: ld returned 1 exit status


I wrote main for it bu still it wont compile please suggest any suggestion for this issue. Thanx in advanced. I wrote my code as below:-



#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > vv;
if(num.size()<3){
return vv;
}

sort(num.begin(), num.end());
// In general, we can make the triples unique by the set trick
// For 3Sum, it is guaranteed by 1, 2, 3
for(int i = 0; i<num.size()-2; i++){
if(i>0 && num[i]==num[i-1]){ // 1
continue;
}
// convert 3Sum to 2Sum
int target = -num[i];
if(target<0){
break;
}
int j = i+1;
int k = num.size()-1;
while(j<k){
if(num[j]+num[k]==target){
vector<int> v;
v.push_back(num[i]);
v.push_back(num[j]);
v.push_back(num[k]);
vv.push_back(v);
}
if(num[j]+num[k]<=target){


// Don't forget ++


while(num[j++]==num[j] && j<k){}; // 2
}
else{
while(num[k--]==num[k] && k>j){}; // 3
}
}
}
return vv;
}
};

Aucun commentaire:

Enregistrer un commentaire