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

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