摘要:和方法一樣,多一個數,故多一層循環。完全一致,不再贅述,
4Sum Problem
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 quadruplets in the array which gives the sum of target.
NoticeElements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is:
(-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)Note
和3Sum方法一樣,多一個數,故多一層循環。完全一致,不再贅述,
Solutionpublic class Solution { public ArrayListUpdate 2018-10> fourSum(int[] A, int target) { int n = A.length; ArrayList > res = new ArrayList(); Arrays.sort(A); for (int i = 0; i < n-3; i++) { if (i != 0 && A[i] == A[i-1]) continue; for (int j = i+1; j <= n-3; j++) { if (j != i+1 && A[j] == A[j-1]) continue; int left = j+1, right = n-1; while (left < right) { int sum = A[i]+A[j]+A[left]+A[right]; if (sum == target) { ArrayList temp = new ArrayList(); temp.add(A[i]); temp.add(A[j]); temp.add(A[left]); temp.add(A[right]); res.add(temp); left++; right--; while (left < right && A[left] == A[left-1]) left++; while (left < right && A[right] == A[right+1]) right--; } else if (sum < target) left++; else right--; } } } return res; } }
class Solution { public List4Sum II Problem> fourSum(int[] nums, int target) { List
> res = new ArrayList<>(); if (nums == null || nums.length < 4) return res; Arrays.sort(nums); int len = nums.length; if (nums[0] * 4 > target || nums[len-1] * 4 < target) return res; for (int i = 0; i < len-3; i++) { if (i != 0 && nums[i] == nums[i-1]) continue; for (int j = i+1; j < len-2; j++) { if (j != i+1 && nums[j] == nums[j-1]) continue; int left = j+1, right = len-1; while (left < right) { int sum = nums[i]+nums[j]+nums[left]+nums[right]; if (sum == target) { List
temp = new ArrayList<>(); temp.addAll(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); res.add(temp); left++; right--; while (left < right && nums[left] == nums[left-1]) left++; while (left < right && nums[right] == nums[right+1]) right--; } else if (sum < target) { left++; } else { right--; } } } } return res; } }
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
Example:
Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
Output:
2
Explanation:
The two tuples are:
(0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
(1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
Solutionclass Solution { public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { int len = A.length; if (len == 0) return 0; Mapmap = new HashMap<>(); for (int a: A) { for (int b: B) { int sum = a+b; map.put(sum, map.getOrDefault(sum, 0)+1); } } int count = 0; for (int c: C) { for (int d: D) { int sum = c+d; if (map.containsKey(-sum)) count += map.get(-sum); } } return count; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65951.html
摘要:找符合條件的總數。雙指針區間考慮邊界,長度,為空,等。之后的范圍用雙指針和表示。若三個指針的數字之和為,加入結果數組。不要求,所以不用判斷了。同理,頭部兩個指針向后推移,后面建立左右指針夾逼,找到四指針和為目標值的元素。 Two Sum Problem Given an array of integers, find two numbers such that they add up ...
摘要:題目詳情輸入一個長度為的整數數組和一個目標整數,我們需要找出是否存在個元素,使得的和等于。如果有,輸出這樣的非重復的元素序列。在求元素的時候可以通過左右指針減少查找時間。 題目詳情 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target...
摘要:解題思路題目要求兩個數和等于,返回其題目說明不會有重復情況,所以我們一旦發現符合情況的,就可以直接結束循環并返回。特殊情況就是正好等于,那肯定是最接近的情況,直接返回即可。 Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific ta...
摘要:這里需要注意及時處理掉重復的情況。那么就需要盡可能排除不可能的情況來提高計算效率。因為數組已經被排序,所以可以根據數組中元素的位置判斷接下來的情況是否有可能合成目標值。 題目要求 此處為原題地址 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d =...
摘要:為了避免得到重復結果,我們不僅要跳過重復元素,而且要保證找的范圍要是在我們最先選定的那個數之后的。而計算則同樣是先選一個數,然后再剩下的數中計算。 2Sum 在分析多數和之前,請先看Two Sum的詳解 3Sum 請參閱:https://yanjia.me/zh/2019/01/... 雙指針法 復雜度 時間 O(N^2) 空間 O(1) 思路 3Sum其實可以轉化成一個2Sum的題,...
閱讀 1751·2021-09-23 11:34
閱讀 2472·2021-09-22 15:45
閱讀 12821·2021-09-22 15:07
閱讀 2221·2021-09-02 15:40
閱讀 4107·2021-07-29 14:48
閱讀 1071·2019-08-30 15:55
閱讀 3245·2019-08-30 15:55
閱讀 2190·2019-08-30 15:55