【LeetCode】15. 3Sum(中等难度)

在这里插入图片描述

解法一 暴力

三层循环,遍历所有的情况。但需要注意的是,我们需要把重复的情况去除掉,就是 [1, -1 ,0] 和 [0, -1, 1] 是属于同⼀种情况的。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        for(int i=0; i<nums.length; i++){
            for(int j=i+1; j<nums.length; j++){
                for(int k=j+1; k<nums.length; k++){
                    if(nums[i] + nums[j] + nums[k] == 0){
                        List<Integer> temp = new ArrayList<Integer>();
                        temp.add(nums[i]);
                        temp.add(nums[j]);
                        temp.add(nums[k]);
                        //判断结果中是否已经有 temp 
                        if(isInList(res, temp)){
                            continue;
                        }
                        res.add(temp);
                    }
                }
            }
        }
        return res;
    }
    public boolean isInList(List<List<Integer>> r, List<Integer> t){
        for(int i=0; i<r.size(); i++){
        	//判断两个 List 是否相同
            if(isSame(r.get(i), t)){
                return true;
            }
        }
        return false;
    }
    public boolean isSame(List<Integer> a, List<Integer> b){
        int count;
        Collections.sort(a);
        Collections.sort(b);
        //排序后判断每个元素是否对应相等
        for(int i=0; i<a.size(); i++){
            if(a.get(i) != b.get(i)){
                return false;
            }
        }
        return true;
    }
}

在这里插入图片描述

解法二

主要思想是,遍历数组,⽤ 0 减去当前的数,作为 sum ,然后再找两个数使得和为 sum。

最最优美的地⽅就是,⾸先将给定的 num 排序。

这样我们就可以⽤两个指针,⼀个指向头,⼀个指向尾,去找这两个数字,这样的话,找另外两个数时间复杂度就会从 O(n²),降到 O(n)。

而要保证不加入重复的 list,我们的 nums 已经有序了,所以只需要找到⼀组之后,当前指针要移到和当前元素不同的地⽅。其次在遍历数组的时候,如果和上个数字相同,也要继续后移。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new LinkedList<>();
        for(int i=0; i<nums.length-2; i++){
            if(i == 0 || (i > 0 && nums[i] != nums[i-1])){
                int l = i + 1, h = nums.length - 1, sum = 0 - nums[i];
                while(l < h){
                    if(nums[l] + nums[h] == sum){
                        res.add(Arrays.asList(nums[i], nums[l], nums[h]));
                        while(l < h && nums[l] == nums[l+1]) l++;
                        while(l < h && nums[h] == nums[h-1]) h--;
                        l++; h--;
                    }else if(nums[l] + nums[h] < sum){
                        l++;
                    }else{
                        h--;
                    }
                }
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玳宸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值