```cpp
class Solution {
public:
static bool comp(int &a, int &b)
{
return a>b;
}
vector<vector<int>> threeSum(vector<int>& nums) {
vector<int> less;
vector<int> more;
vector<vector<int>> ans;
int n = nums.size();
int zeronum = 0;
for (int i = 0; i < n; i++)
{
if (nums[i] == 0)
{
zeronum++;
}
if (nums[i] < 0)
{
less.push_back(nums[i]);
//printf("%d ", nums[i]);
}
if (nums[i] > 0)
{
more.push_back(nums[i]);
//printf("%d ", nums[i]);
}
}
//printf(" end\n");
if (zeronum >= 3) ans.push_back({0, 0, 0});
int lessnum = less.size(), morenum = more.size();
if (lessnum == 0 || morenum == 0) return ans;
sort(less.begin(), less.end());
sort(more.begin(), more.end(), comp);
// for (int i = 0; i < lessnum; i++)
// {
// printf("%d ", less[i]);
// }
// printf(" ~~\n");
// for (int i = 0; i < morenum; i++)
// {
// printf("%d ", more[i]);
// }
// printf("!!\n");
for (int i = 0; i < lessnum; i++)
{
if (i > 0 && less[i] == less[i - 1]) continue;
for (int j = 0; j < morenum; j++)
{
if (j > 0 && more[j] == more[j - 1]) continue;
if (more[j] > abs(less[i])) continue;
if (more[j] + less[i] == 0 && zeronum > 0)
{
// vector<int> v{less[i], 0, more[j]};
// ans.push_back(v);
// printf("yes\n");
ans.push_back({less[i], 0, more[j]});
//printf("1: %d 0 %d\n", less[i], more[j]);
}
for (int z = j + 1; z < morenum; z++)
{
if (z > j + 1 && more[z] == more[z - 1]) continue;
if (more[j] + more[z] + less[i] == 0)
{
ans.push_back({more[j], more[z], less[i]});
// printf("2: %d %d %d\n", more[j], more[z], less[i]);
}
}
}
}
// if (more[0] > abs(less[0]))
// {
for (int i = 0; i < morenum; i++)
{
if (i > 0 && more[i] == more[i - 1]) continue;
for (int j = 0; j < lessnum; j++)
{
// if (abs(less[j]) >= more[i]) break;
if (j > 0 && less[j] == less[j - 1]) continue;
// if (more[i] + less[j] == 0 && zeronum > 0)
// {
// ans.push_back({less[j], 0, more[i]});
// printf("%d 0 %d\n", less[j], more[i]);
// }
for (int z = j + 1; z < lessnum; z++)
{
if (z > j + 1 && less[z] == less[z - 1]) continue;
if (less[j] + less[z] + more[i] == 0)
{
ans.push_back({more[i], less[z], less[j]});
// printf("3: %d %d %d\n", more[i], less[z], less[j]);
}
}
}
}
// }
return ans;
}
};
class Solution {
public:
static bool comp(int &a, int &b)
{
return a>b;
}
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> ans;
int n = nums.size();
sort(nums.begin(), nums.end());
for(auto i : nums) cout << i << " ";
cout << "end" << endl;
for (int i = 0; i < n; i++)
{
if (nums[i] > 0) break;
if (i > 0 && nums[i] == nums[i - 1]) continue;
int l = i + 1, r = n - 1;
while (l < r)
{
if (nums[i] + nums[l] + nums[r] == 0)
{
ans.push_back({nums[i], nums[l], nums[r]});
printf("%d %d %d\n", nums[i], nums[l], nums[r]);
while (l < r && nums[l + 1] == nums[l]) l++; // not overflow
while (l < r && nums[r - 1] == nums[r]) r--;
l++, r--;
}
else if (nums[i] + nums[l] + nums[r] > 0) r--;
else if (nums[i] + nums[l] + nums[r] < 0) l++;
}
}
return ans;
}
};