【无标题】

该C++代码定义了一个Solution类,包含一个threeSum方法,用于在一个整数数组中找到三个数,使它们的和等于0。方法首先处理零元素,然后对负数和正数分别进行排序,通过双指针法寻找符合条件的三元组。
摘要由CSDN通过智能技术生成

```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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值