1. 程式人生 > >[LeetCode] 18. 4Sum

[LeetCode] 18. 4Sum

思路:
做過3Sum的同學看到這道題的話肯定不會感到困難, 只是在3Sum的基礎上再新增一層迴圈, 時間複雜度O(n^3), 但是根據https://discuss.leetcode.com/topic/28641/my-16ms-c-code的機智思路, 可以在每層迴圈開始前做幾次非常機智的判斷, 如果前幾個數的和就大於目標值了, 後面肯定更大了, 直接跳出迴圈. 如果當前值加上尾部的幾個值小於目標值, 那麼也不用進內層迴圈了, 也直接做下一次迴圈就好, 這樣節省了大量時間, 真是學習到了! 以後要多注意剪枝

vector<vector<int>> fourSum(vector
<int>
& nums, int target) { vector<vector<int>> res; int size = nums.size(); if (size < 4) return res; sort(nums.begin(), nums.end()); for (int i = 0; i < size - 3; i++) { if (i && nums[i] == nums[i - 1]) continue; // 精髓之處 if
(nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break; if (nums[i] + nums[size - 1] + nums[size - 2] + nums[size - 3] < target) continue; // vector<int> candidate; for (int j = i + 1; j < size - 2; j++) { if (j > i + 1 && nums[j] == nums[j - 1
]) continue; // 同樣精髓之處 if (nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break; if (nums[i] + nums[j] + nums[size - 1] + nums[size - 2] < target) continue; // int TwoSumTarget = target - nums[i] - nums[j]; int head = j + 1, tail = size - 1; while (head < tail) { if (nums[head] + nums[tail] == TwoSumTarget) { candidate.push_back(nums[i]); candidate.push_back(nums[j]); candidate.push_back(nums[head]); candidate.push_back(nums[tail]); res.push_back(candidate); candidate.clear(); while (head < tail && nums[head + 1] == nums[head]) head++; while (head < tail && nums[tail - 1] == nums[tail]) tail--; head++, tail--; } else if (nums[head] + nums[tail] < TwoSumTarget) head++; else tail--; } } } return res; }

相關推薦

LeetCode-18-4Sum

4sum 一個數 lee 麻煩 tco target 解決 一點 i++ 一、問題描述   給定一個數組S,和一個int類型的數target,在S中尋找四個數,這四個數之和為target。返回一個vector<vector<int>>   例子:S=

[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

LeetCode 18. 4Sum

文章 相等 return 代碼 復雜 避免 ont 易懂 博客園 問題鏈接 LeetCode 18. 4Sum 題目解析 給定n元素的數組,求出滿足 \(a+b+c+d = target\) 的所有元組。 解題思路 已經成為一個系列了,這道題是 LeetCode 15. 3

leetcode -18.4Sum

我的解法是利用2Sum相同的演算法: 1. 對原陣列排序 2. 設定首尾兩個指標。當兩數的和大於target時,尾指標向左移一位;當兩數的和小於target時,尾指標向右移一位。 對於4Sum的解法,因為是四個數字,利用兩個迴圈陣列確定前兩個數字,剩下的兩個數字和target便可以使用2

[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. 4Sum

15題的變式 題意: 給出一個數組和目標和target,在陣列中找到四個數,使他們相加之和為target 我的思路: 雙層迴圈(最優解法即為O(N^2^)) 51%, O(N^2^) 類似15題,先排序,外層是兩層迴圈,內層是兩個指標找剩餘兩個數的和 步

[leetcode]18. 4Sum

這一題和那個3sum是一個思路。其實多少sum都是一樣的。 有兩種方法解,明顯第二種比較快。 Solution 1: hashmap+三個迴圈 空間複雜度為O(n) 時間複雜度為O(n3) class Solution { public List<List<I

leetcode 18. 4Sum

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

leetcode-18.4Sum 四數之和

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

leetcode --18. 4Sum

題目:https://leetcode.com/problems/4sum/description/ 程式碼:class Solution { public: vector<vector<int>> fourSum(vector<int

[LeetCode] 18. 4Sum

思路: 做過3Sum的同學看到這道題的話肯定不會感到困難, 只是在3Sum的基礎上再新增一層迴圈, 時間複雜度O(n^3), 但是根據https://discuss.leetcode.com/topic/28641/my-16ms-c-code

leetcode 18. 4Sum KSum的解決辦法

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplet

Array + two points leetcode.18 - 4Sum

while urn 排序 pub ger arr 代碼 思路 array 題面 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nu

LeetCode18. 4Sum(Medium)

res 丟失 src 開始 整數 ati lan light ref 1. 原題鏈接 https://leetcode.com/problems/4sum/description/ 2. 題目要求 給出整數數組S[n],在數組S中是否存在a,b,c,d四個整數,使得四個數

LeetCode Notes_#18 4Sum

LeetCode Notes_#18 4Sum LeetCode  Contents 題目 思路和解答 思路 解答

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

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

LeetCode18. 4Sum - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given an array nums of n integers and an integer target, are there eleme

leetcode18. 4Sum(C)

Description: Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b +

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

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

leetcode18. 4Sum

18. 4Sum Problem Solution (1240 ms) python C (quick_sort : 24 ms bubble_sort :24 ms) Problem Given an a