# Simply breaking the code in different functions for every part (easy c++ code)

• ``````class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(),num.end());
vector<vector<int> > ans;
for(int i=0;i<num.size();i++)
{
if(i>0 && num[i-1]==num[i])
continue;
getsolutions(num,i,ans);
}
return ans;
}
void getsolutions(vector<int>&num, int i,vector<vector<int> > &ans)
{
int start=i+1,end=num.size()-1,sum;
while(start<end)
{
sum  = num[start]+num[end]+num[i];
if(sum==0)
{
addsolutions(start,end,i,num,ans);
start++;
end--;
}
else if(sum>0)
end--;
else
start++;
}
}
void addsolutions(int &start,int &end ,int &i,vector<int>&num ,vector<vector<int> >&ans)
{
int n = ans.size()-1;
if(n>=0 && ans[n][0]==num[i] &&ans[n][1]==num[start] &&ans[n][2]==num[end])
return ;
vector<int> k;
k.push_back(num[i]);
k.push_back(num[start]);
k.push_back(num[end]);
ans.push_back(k);
}

};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.