成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 4Sum & 4Sum II

sydMobile / 3484人閱讀

摘要:和方法一樣,多一個(gè)數(shù),故多一層循環(huán)。完全一致,不再贅述,

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方法一樣,多一個(gè)數(shù),故多一層循環(huán)。完全一致,不再贅述,

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;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65951.html

相關(guān)文章

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

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

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

    摘要:題目詳情輸入一個(gè)長度為的整數(shù)數(shù)組和一個(gè)目標(biāo)整數(shù),我們需要找出是否存在個(gè)元素,使得的和等于。如果有,輸出這樣的非重復(fù)的元素序列。在求元素的時(shí)候可以通過左右指針減少查找時(shí)間。 題目詳情 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

    摘要:解題思路題目要求兩個(gè)數(shù)和等于,返回其題目說明不會有重復(fù)情況,所以我們一旦發(fā)現(xiàn)符合情況的,就可以直接結(jié)束循環(huán)并返回。特殊情況就是正好等于,那肯定是最接近的情況,直接返回即可。 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

    摘要:這里需要注意及時(shí)處理掉重復(fù)的情況。那么就需要盡可能排除不可能的情況來提高計(jì)算效率。因?yàn)閿?shù)組已經(jīng)被排序,所以可以根據(jù)數(shù)組中元素的位置判斷接下來的情況是否有可能合成目標(biāo)值。 題目要求 此處為原題地址 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 多數(shù)和

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

    trigkit4 評論0 收藏0

發(fā)表評論

0條評論

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