3个数字相加等于0,时间复杂度O(n2)

3位数字相加等于0
题目
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: 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]
]


分析
给出一个数组,要找出三位数相加等于0的组合,但是结果中不能包含重复的组合。 
一开始我们都会想到穷举法,时间复杂度很明显是O(n^3),那怎样才能优化我们的代码呢?算法
首先对数组进行排序。
从左向右遍历数组,每次都固定一个数,然后剩下的两个数从左边与从右边向中间靠拢得到。类似于这里面的方法,具体看下面的代码。
代码

public List<List<Integer>> threeSum(int[] num) {
    Arrays.sort(num);//排序
    List<List<Integer>> res = new LinkedList<>(); 
    for (int i = 0; i < num.length-2; i++) {
        if (i == 0 || (i > 0 && num[i] != num[i-1])) {
            //两个指针  一个从i+1  一个从最右边 向中间靠拢
            int lo = i+1, hi = num.length-1, sum = 0 - num[i];
            while (lo < hi) {
                if (num[lo] + num[hi] == sum) {
                    res.add(Arrays.asList(num[i], num[lo], num[hi]));
                    //跳过重复项(已经是排好序的)如:
                    //-5 -4 -4 -4 -1 2 3 4 9 9 9 10
                    //假如i=0  lo = 1  hi = 10  
                    //此时num[i] = -5; num[lo] = -4;num[hi] = 9;
                    //lo从1跳到3
                    while (lo < hi && num[lo] == num[lo+1]) lo++;
                    //hi从10跳到8
                    while (lo < hi && num[hi] == num[hi-1]) hi--;

                    //lo与hi同时变化的原因:
                    //只变化一个一定不满足num[lo] + num[hi] == sum
                    //所以两个都变化能够减少一次无用的比较
                    lo++; hi--;
                } else if (num[lo] + num[hi] < sum) lo++;
                else hi--;
           }
        }
    }
    return res;
    }


时间复杂度为O(n^2),改善了很多。
--------------------- 
作者:CSU_ICELEE 
来源:CSDN 
原文:https://blog.csdn.net/q1242027878/article/details/54983416 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值