LeetCode刷题记|4|三数之和

154 阅读1分钟

本文已参与「新人创作礼」活动, 一起开启掘金创作之路。

题目

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

注意:

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

 

示例 1:

输入:nums = [-1,0,1,2,-1,-4]

输出:[[-1,-1,2],[-1,0,1]]

示例 2:

输入:nums = []

输出:[]

示例 3:

输入:nums = [0]

输出:[]  

提示:

0 <= nums.length <= 3000

-105 <= nums[i] <= 105

思路:

(参考下方答案解决): 排序 2、 三个数字,保证第一个数字不重复。调用twoSum() 3、 twoSum(nums, lo, target) : 返回两个值的list, 每个list中的两个值加起来=target (需要注意while循环放置的位置)

4、小优化,当nums[i]>0时,则没必要继续调用twoSum,因为两个大于0的数加起来不可能 < 0

答案

  public static List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                break;
            }

            List<List<Integer>> list = twoSum(nums, i + 1, -nums[i]);

            if (!list.isEmpty()) {
                for (List<Integer> integers : list) {
                    integers.add(nums[i]);
                    res.add(integers);
                }
            }

            while (i + 1 < nums.length && nums[i] == nums[i + 1]) {
                i++;
            }
        }

        return res;
    }

    public static List<List<Integer>> twoSum(int[] nums, int start, int target) {
        List<List<Integer>> res = new ArrayList<>();
        int lo = start;
        int hi = nums.length - 1;
        while (lo < hi) {
            int left = nums[lo];
            int right = nums[hi];
            if (left + right < target) {
                lo++;
            } else if (left + right > target) {
                hi--;
            } else {
                List<Integer> list = new ArrayList<>();
                list.add(nums[lo]);
                list.add(nums[hi]);

                res.add(list);
                while (nums[lo] == left && lo < hi) {
                    lo++;
                }
                while (nums[hi] == right && lo < hi) {
                    hi--;
                }
            }
        }
        return res;
    }


显而易见,这种算法内存消耗太大,于是我自己想了一下,尝试降低内存消耗

思路2:

参考双指针解决,参考作者:数据结构和算法

public List<List<Integer>> threeSum(int[] num) {
    //先对数组进行排序
    Arrays.sort(num);
    List<List<Integer>> res = new ArrayList<>();
    for (int i = 0; i < num.length - 2; i++) {
        //过滤掉重复的
        if (i > 0 && num[i] == num[i - 1])
            continue;
        //因为是排序的,如果第一个数字大于0,那么后面的也都
        //大于0,他们三个数字的和不可能等于0
        if (num[i] > 0)
            break;
        int left = i + 1;//左指针
        int right = num.length - 1;//右指针
        int target = -num[i];
        while (left < right) {
            //左右指针的和
            int sum = num[left] + num[right];
            if (sum == target) {
                //找到了一组,把他们加入到集合list中
                res.add(Arrays.asList(num[i], num[left], num[right]));
                //过滤掉重复的
                while (left < right && num[left] == num[left + 1])
                    left++;
                while (left < right && num[right] == num[right - 1])
                    right--;
                left++;
                right--;
            } else if (sum < target) {
                left++;
            } else {
                right--;
            }
        }
    }
    return res;
}