1. 程式人生 > >18. 四數之和

18. 四數之和

知乎ID: 碼蹄疾
碼蹄疾,畢業於哈爾濱工業大學。
小米廣告第三代廣告引擎的設計者、開發者;
負責小米應用商店、日曆、開屏廣告業務線研發;
主導小米廣告引擎多個模組重構;
關注推薦、搜尋、廣告領域相關知識;

題目

給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。

注意:

答案中不可以包含重複的四元組。

示例:

給定陣列 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

滿足要求的四元組集合為:

[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

題解

三個數之和轉化為2個數之和來做,那四個數之和轉化為3個數之和來做即可。
先看下三個數之和等於0的程式碼:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if
(nums.length < 3) { return res; } Arrays.sort(nums); for (int i = 0; i < nums.length; i++) { //相同,跳過 if (i > 0 && nums[i] == nums[i - 1]) { continue; } int l = i + 1; int r = nums.length - 1
; while (l < r) { int sum = nums[i] + nums[l] + nums[r]; if (sum == 0) { res.add(Arrays.asList(nums[i], nums[l], nums[r])); //相同,跳過 while (l < r && nums[l] == nums[l + 1]) l++; while (l < r && nums[r] == nums[r - 1]) r--; l++; r--; } else if (sum < 0) { l++; } else { r--; } } } return res; } }

那麼四個數無非就是加一層for迴圈,值得注意的是,四個數中,第一個數、第三個數、第四個數的去重處理可以直接照搬上面的程式碼,加一下第二個數的去重處理即可。

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if (nums.length < 4) {
            return res;
        }
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            for (int j = i + 1; j < nums.length; j++) {
                //第二個數的去重處理
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                int l = j + 1;
                int r = nums.length - 1;
                while (l < r) {
                    int sum = nums[i] + nums[j] + nums[l] + nums[r];
                    if (sum == target) {
                        res.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
                        while (l < r && nums[l] == nums[l + 1]) l++;
                        while (l < r && nums[r] == nums[r - 1]) r--;
                        l++;
                        r--;
                    } else if (sum < target) {
                        l++;
                    } else {
                        r--;
                    }
                }
            }
        }
        return res;
    }
}

相關題目

相關推薦

[Leetcode 18]之和 4 Sum

ive else lis 一次 arrays int margin tro integer 【題目】 Given an array nums of n integers and an integer target, are there elements a, b, c, a

LeetCode 18 之和 (4sum) —— 關於二維動態陣列的初始化

本題中,二維動態陣列的初始化。 本題每一行元素的數量是確定的,而不確定有幾列的情況 vector s(4,0);//每一行元素,定義為一維陣列 vector<vector> result;//定義一個二維陣列 //選出每一個s的過程 result.push_back(s);/

Leetcode(18)之和

題目描述 給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。 注意: 答案中不可以包含重複的四元組。 示例: 給定陣列

【LeetCode】18. 之和

題目描述 給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?

leetcode 18:之和

四數之和與兩數之和,三數之和leetcode 15類似,最後都要到兩數之和,通過s++,t--來降低時間複雜度 std::vector<std::vector<int>> fourSum(std::vector<int>& n

[Leetcode] 18. 之和 java

 給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。 注意: 答案中不可以包含重複的四元組。 示

LeetCode 18. 之和 4Sum(C語言)

題目描述: 給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。 注意: 答案中不可以包含重複的四元組。

Leetcode演算法Java全解答--18. 之和

Leetcode演算法Java全解答–18. 四數之和 題目 給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有

18. 之和 Java

問題描述 給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。 注意: 答案中不可以

Leetcode 18:之和(最詳細解決方案!!!)

給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。 注意: 答案中不可以包含重複的四元組。 示例:

18. 之和

知乎ID: 碼蹄疾 碼蹄疾,畢業於哈爾濱工業大學。 小米廣告第三代廣告引擎的設計者、開發者; 負責小米應用商店、日曆、開屏廣告業務線研發; 主導小米廣告引擎多個模組重構; 關注推薦、搜尋、廣告領域相關知識; 題

LeetCode 18.之和

跟三數之和的解法差不多,只是多了一重for迴圈,現在時間複雜度為O(n^3)。 import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class

[LeetCode] 18. 4Sum 之和

let bsp elif bject als body 空間 uniq 在外 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = t

LeetCode15 18 16三之和 之和 最接近的三之和 (陣列)

1.三數之和 給定一個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重複的三元組。 注意:答案中不可以包含重複的三元組。 例如, 給定陣列 nums = [-1, 0, 1, 2, -1

[leetcode]18. 4Sum之和

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums&n

Leetcode演算法——18之和

題目 給定一個長度為n的整數陣列,一個目標整數target。 在陣列中找到4個數a,b,c,d,使得 a+b+c+d=target。 要求找到所有不重複的四元組合。 示例: Given array nums = [1, 0, -1, 0, -2, 2], and

leetcode-18.4Sum 之和

題目: Given an array nums of n integers and an integer target, are there elements a, b, c, and&nb

LeetCode演算法題18之和解析

給定一個包含 n 個整數的陣列 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。 注意: 答案中不可以包含重複的四元組。 示例: 給定陣

領釦題庫18題:之和

C++暴力求解,在三數之和的基礎上增加一個迴圈 。(非正確答案,只是提供一下暴力求解的原理) class Solution { public: vector<vector<int>> fourSum(vector<int>&

LeetCode第18題:之和(JAVA實現)

題目: 我的解答: public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); List<List<Integer>&