class Solution {
public:
vector<vector<int> > threeSum(vector<int> &nums) {
const int n = nums.size();
if (n < 3) return {};
sort(begin(nums), end(nums));
vector<vector<int>> sols;
for (int i = 0; i < n - 2; ++i) {
if (i and nums[i] == nums[i - 1]) continue;
int j = i + 1, k = n - 1;
while (j < k) {
if (nums[j] + nums[k] < -nums[i]) ++j;
else if (nums[j] + nums[k] > -nums[i]) --k;
else {
sols.push_back({nums[i], nums[j], nums[k]});
while (j < k and nums[j + 1] == nums[j]) ++j;
while (j < k and nums[k - 1] == nums[k]) --k;
++j, --k;
}
}
}
return sols;
}
};