class Solution {
public:
vector<vector<int>> threeSum(vector<int>& num) {
vector<vector<int>> res;
int size = num.size();
if (size < 3) {
return res;
}
sort(num.begin(), num.end());
for (int i = 0; i < size - 2; i++) {
int a = num[i];
if (a > 0) {
break;
}
if (i > 0 && a == num[i -1]){
continue;
}
int j = i + 1;
int k = size - 1;
while (j < k) {
int b = num[j];
int c = num[k];
int sum = a + b + c;
if (sum == 0){
res.push_back({a, b, c});
while(b == num[++j]);
while(c == num[--k]);
} else if (sum > 0){
k--;
} else {
j++;
}
}
}
return res;
}
};