# Memory Limit Exceeded

• Hi, I tried implementing the algorithm on wikipedia, but I get Memory limit exceeded. I understand there are too many duplicates in my output. Below is my code. Could you help me iron out the duplicates, I am quite confused.

`````` class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n= num.size() ;
vector<vector<int>> res;
vector<int> triplet;
int a, b, c, k,l;
// int inc =0;
if(n<3) return res;
sort(num.begin(), num.end());
for(int i=0; i<n-2;++i)
{
a=num[i];
k= i+1;
l = n-1;
while(k<l){
b= num[k];
c= num[l];
if(a+b+c==0)
{
triplet.clear();
triplet.push_back(a);
triplet.push_back(b);
triplet.push_back(c);
res.push_back(triplet);
continue;
}
else if((a+b+c)<0) k=k+1;
else
l=l-1;
}
}
return res;
}
};``````

• ``````class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n= num.size() ;
vector<vector<int>> res;
vector<int> triplet;
int a, b, c, k,l;
if(n<3) return res;
sort(num.begin(), num.end());
for(int i=0; i<n-2;++i) {
if(i>0 && num[i] == num[i-1])
continue; // Skip same a
a=num[i];
k= i+1;
l = n-1;
while(k<l) {
b= num[k];
c= num[l];
if(a+b+c==0) {
triplet.clear();
triplet.push_back(a);
triplet.push_back(b);
triplet.push_back(c);
res.push_back(triplet);
l=l-1;
while(k<l&&num[l]==num[l+1]) // Skip same c
l--;
k=k+1;
while(k<l&&num[k]==num[k-1]) // Skip same b
k++;
}
else if((a+b+c)<0) {
k=k+1;
while(k<l&&num[k]==num[k-1]) // Skip same b
k++;
}
else {
l=l-1;
while(k<l&&num[l]==num[l+1]) // Skip same c
l--;
}
}
}
return res;
}
};``````

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