国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 4Sum & 4Sum II

sydMobile / 3471人閱讀

摘要:和方法一樣,多一個數,故多一層循環。完全一致,不再贅述,

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.

Notice

Elements 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.

Example

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方法一樣,多一個數,故多一層循環。完全一致,不再贅述,

Solution
public class Solution {
    public ArrayList> 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;
    }
}
Update 2018-10
class Solution {
    public List> 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;
    }
}
4Sum II Problem

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

Solution
class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        int len = A.length;
        if (len == 0) return 0;
        Map map = 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

相關文章

  • 【LC總結】K Sum (Two Sum I II/3Sum/4Sum/3Sum Closest)

    摘要:找符合條件的總數。雙指針區間考慮邊界,長度,為空,等。之后的范圍用雙指針和表示。若三個指針的數字之和為,加入結果數組。不要求,所以不用判斷了。同理,頭部兩個指針向后推移,后面建立左右指針夾逼,找到四指針和為目標值的元素。 Two Sum Problem Given an array of integers, find two numbers such that they add up ...

    awesome23 評論0 收藏0
  • leetcode 18 4Sum

    摘要:題目詳情輸入一個長度為的整數數組和一個目標整數,我們需要找出是否存在個元素,使得的和等于。如果有,輸出這樣的非重復的元素序列。在求元素的時候可以通過左右指針減少查找時間。 題目詳情 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target...

    Vixb 評論0 收藏0
  • [Leetcode] Two Sum, 3Sum,4Sum,4SumII,3Sum Closet

    摘要:解題思路題目要求兩個數和等于,返回其題目說明不會有重復情況,所以我們一旦發現符合情況的,就可以直接結束循環并返回。特殊情況就是正好等于,那肯定是最接近的情況,直接返回即可。 Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific ta...

    EddieChan 評論0 收藏0
  • leetcode14 4sum

    摘要:這里需要注意及時處理掉重復的情況。那么就需要盡可能排除不可能的情況來提高計算效率。因為數組已經被排序,所以可以根據數組中元素的位置判斷接下來的情況是否有可能合成目標值。 題目要求 此處為原題地址 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d =...

    MoAir 評論0 收藏0
  • [Leetcode] 3Sum 4Sum 3Sum Closet 多數和

    摘要:為了避免得到重復結果,我們不僅要跳過重復元素,而且要保證找的范圍要是在我們最先選定的那個數之后的。而計算則同樣是先選一個數,然后再剩下的數中計算。 2Sum 在分析多數和之前,請先看Two Sum的詳解 3Sum 請參閱:https://yanjia.me/zh/2019/01/... 雙指針法 復雜度 時間 O(N^2) 空間 O(1) 思路 3Sum其實可以轉化成一個2Sum的題,...

    trigkit4 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<