[LeetCode] 3Sum

本文探讨了如何寻找数组中三个整数相加为零的所有唯一组合,通过排序和去重的方法优化了三重循环的基本思路,实现了高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[Problem]

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)

The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:

(-1, 0, 1)

(-1, -1, 2)

[Analysis]

直观的想法,使用3重循环,判断选出来的三个数之和是否等于0,如果等于0,则将这三个数排序,判断结果集中是否已经存在该结果,如果没有,则加入结果集。复杂度O(n^3),跑Judge_Small没问题,但是跑Judge_Large是会TLE的;

分析上述问题:
(1) 输出每个结果排序的问题:其实可以先对整个num排序,然后输出的时候先输出外层循环的值,再输出中层循环的值,最后输出内层循环的值,这样就保证了输出的每个结果都是有序的;

(2) 输出结果判重的问题:既然已经对整个num排过序了,对于每一层循环,如果当前循环指向的数值与后面的若干个数值相同,则可以直接将指针指向下一个不相同的数值。例如:对于排序后的num[-1, -1, -1, 0, 0, 0, 1, 1, 1],用i,j,k表示循环的下标,当i=0, j=3, k=6时,判断num[i]+num[j]+num[k]=0,将结果写入,进入下一个循环。这时发现,num[k]后面有多个相同的数,则直接将k移到最后;同样,对于j而言,下一次循环,直接另j=6,而不用另j=4,然后再进行一次k=[6,7,8];对于i也是一样。这样做的优点,去重、剪枝,过Judge_Large也没问题了。


[Solution]

class Solution {
    public:
        vector<vector<int> > threeSum(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
       
        int i = 0, j = 0, k = 0, m = 0;
        vector<vector<int> > res;
        if(num.size() <= 0)
            return res;
       
        // not null
        sort(num.begin(), num.end());
        while(i <= num.size() - 3){
            // the first number is bigger than 0
            if(num[i] > 0)break;
           
            // add the second number
            j = i + 1;
            while(j <= num.size() - 2){
                // the sum of the first two number is bigger than 0
                if(num[i] + num[j] > 0)break;
               
                // add the third number
                k = j + 1;
                while(k <= num.size() - 1){
                    // the sum of the three number is bigger than 0
                    if(num[i] + num[j] + num[k] > 0)break;
                   
                    // the sum of the three number equals to 0, add to result
                    if(num[i] + num[j] + num[k] == 0){
                        vector<int> tmp;
                        tmp.push_back(num[i]);
                        tmp.push_back(num[j]);
                        tmp.push_back(num[k]);
                        res.push_back(tmp);
                    }
                   
                    // move forward for same num[k]
                    m = num[k];
                    k++;
                    while(k <= num.size() - 1 && num[k] == m){
                        k++;
                    }
                }
               
                // move forward for same num[j]
                m = num[j];
                j++;
                while(j <= num.size() - 2 && num[j] == m){
                    j++;
                }
            }
           
            // move forward for same num[i]
            m = num[i];
            i++;
            while(i <= num.size() - 3 && num[i] == m){
                i++;
            }
        }
        return res;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值