LeetCode题解(Java实现)——15. 3Sum(三数之和)

前言

欢迎关注我的 Github,如果觉得有帮助,请点个 star 哟,目前主要在更 leetcode题解(Java版)剑指offer题解(Java版),可以点个star

文本已收录至我的GitHub仓库,欢迎Star:awesome-java-notes

3Sum

问题描述

给定 n 个整数的数组 nums,是否有元素 a,b,c 在 nums 中,使得 a + b + c = 0?找到数组中所有唯一的三元组,使得它们的总和为零。

leetCode 地址:LeetCode15. 3Sum

注意

解决方案集中不得包含重复的三元组。

示例

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

解法一

解题思路:常规暴力解法,依次遍历数组找到对应的 a,b,c 三个元素,并判断这三个数的和是否为 0,如果为 0 则符合条件。不过,这种直接的暴力解法时间复杂度是比较高的,执行的次数为 N(N-1)(N-2)=N3/6-N2/2+N/3,近似为 N3/6N^3 / 6 ,时间复杂度为 O(N3)O(N^3)

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int N = nums.length;
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                for (int k = j + 1; k < N; k++) {
                    if (nums[i] + nums[j] + nums[k] == 0) {
                        res.add(Arrays.asList(nums[i], nums[j], nums[k]));
                    }
                }
            }
        }
        return res;
    }
}

【注】应该注意的是,只有数组不含有相同元素才能使用这种解法,否则结果会出错。

解法二

解题思路:先将数组排序,然后对两个元素求和,并利用二分查找法查找是否存在这两数之和的相反数,如果存在则说明存在满足条件的三元组。这种解法的时间复杂度为:O(N2logN)O(N^2logN)

class Solution {

    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int N = nums.length;
        Arrays.sort(nums);
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                int target = -nums[i] - nums[j];
                int index = BinarySearch.search(nums, target);
                // 这里需要注意 index 必须大于 j,否则会重复计数
                if (index > j) {
                	// 将符合条件的三元组封装到集合中
                    res.add(Arrays.asList(nums[i], nums[j], nums[index]));
                }
            }
        }
        return res;
    }
}

class BinarySearch {
    public static int search(int[] nums, int target) {
        int l = 0, h = nums.length - 1;
        while (l <= h) {
           int mid = l + (h - l) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                h = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return -1;
    }
}

【注】应该注意的是,只有数组不含有相同元素才能使用这种解法,否则结果会出错。

解法三

解题思路:现将数组排序,然后利用双指针的解法来解决,时间复杂度为: O(N2)O(N^2)

class Solution {

    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int N = nums.length;
        Arrays.sort(nums);
        for (int i = 0; i < N - 2; i++) {
            int l = i + 1;
            int h = N - 1;
            int target = -nums[i];
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            while (l < h) {
                int sum = nums[l] + nums[h];
                if (sum == target) {
                    res.add(Arrays.asList(nums[i], nums[l], nums[h]));
                    // nums[l] 与 nums[l + 1]重复,直接 l++ 跳到下一位
                    while (l < h && nums[l] == nums[l + 1]) l++;
                    // nums[h] 与 nums[h - 1]重复,直接 h-- 跳到前一位
                    while (l < h && nums[h] == nums[h - 1]) h--;
                    l++;
                    h--;
                } else if (sum < target) {
                    l++;
                } else {
                    h--;
                }
            }
        }
        return res;
    }
}

结语

如果你同我一样想要征服数据结构与算法、想要刷 LeetCode,欢迎关注我 GitHub 上的 LeetCode 题解:awesome-java-notes

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章