LeetCode哈希表篇【三数之和】

力扣题目链接(opens new window)

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。

注意: 答案中不可以包含重复的三元组。

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]

#思路

一个数组,从中找三个数使其和为0;那么基本思路就是将其排序,从两头找数然后寻找数组中有无使条件满足的第三位。

一开始我是想着满足条件无非三种情况:0+0+0;正+正+负;负+负+正。让正+正+负中的负为寻找的第三位数(或负+负+正中的正为寻找的第三位数),但写着写着感觉代码有点太繁琐,就决定另寻它路......

首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。

依然还是在数组中找到 abc 使得a + b +c =0,我们这里相当于 a = nums[i] ,b = nums[left], c = nums[right]。

接下来如何移动left 和right呢, 如果nums[i] + nums[left] + nums[right] > 0 就说明 此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。

如果 nums[i] + nums[left] + nums[right] < 0 说明 此时 三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。

时间复杂度:O(n^2)。

class Solution {
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        
        Set<List> set = new HashSet<>();
        int index = nums.length - 1;
        if (nums.length < 3 || nums[0] > 0 || nums[nums.length - 1] < 0) {
            return list;
        }

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                index = i;
                break;
            }
        }
        for (int i = 0; i < index; i++) {
            if (i <= nums.length - 3) {
                int start = i + 1;
                int end = nums.length - 1;
                while (start != end) {
                    if (nums[start] + nums[end] == -nums[i]) {
                        List<Integer> temp = new ArrayList<>();
                        temp.add(nums[i]);
                        temp.add(nums[start]);
                        temp.add(nums[end]);
                        if(!list.contains(temp)){
                            list.add(temp);
                        }
                        end--;
                    }else if (nums[start] + nums[end] >-nums[i]){
                        end--;
                    }else if(nums[start] + nums[end] <-nums[i]){
                        start++;
                    }
                }
            }
        }
        return list;
    }
}

这里经过实验发现:使用 if(!list.contains(temp))这个方法,会为我们的方法平白的加上一层O(n),

所以我们要想办法去解决这个问题;我们可以在fori过程和双指针移动的过程去删去重复元素,进而不用去进行查重的判断:

class Solution {
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        
        Set<List> set = new HashSet<>();
        int index = nums.length - 1;
        if (nums.length < 3 || nums[0] > 0 || nums[nums.length - 1] < 0) {
            return list;
        }

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                index = i;
                break;
            }
        }
        for (int i = 0; i < index; i++) {
            if (i <= nums.length - 3) {
                int start = i + 1;
                int end = nums.length - 1;
                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }

                while (start != end) {
                    if (nums[start] + nums[end] >-nums[i]||(end!=nums.length-1&&nums[end]==nums[end+1])){
                        end--;
                    }else if(nums[start] + nums[end] <-nums[i]||(start!=i+1&&nums[start]==nums[start-1])){
                        start++;
                    }else if (nums[start] + nums[end] == -nums[i]) {
                        list.add(Arrays.asList(nums[i], nums[start], nums[end]));
                        end--;
                    }
                }
            }
        }
        return list;
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值