[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? Find all unique quadruplets in the array which gives the sum of target.Note: The s

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.

Note: The solution set must not contain duplicate quadruplets.

For 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]
]

借鉴三个数的方法,固定两个数,再用两个指针去搜寻。

public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        int len = nums.length;
        Arrays.sort(nums);
        if (len < 4)
            return res;
        for (int i = 0; i < len - 1; i++) {
            if (i > 0 && nums[i] == nums[i - 1])
                continue;
            for (int j = i + 1; j < len; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1])
                    continue;
                int begin = j + 1, end = len - 1;
                while (begin < end) {
                    int sum = nums[i] + nums[j] + nums[begin] + nums[end];
                    if (sum == target) {
                        List<Integer> list = new ArrayList<Integer>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[begin]);
                        list.add(nums[end]);
                        res.add(list);
                        begin++;
                        end--;
                        while (begin < end && nums[begin] == nums[begin - 1])
                            begin++;
                        while (begin < end && nums[end] == nums[end + 1])
                            end--;
                    } else if (sum > target)
                        end--;
                    else
                        begin++;
                }
            }
        }
        return res;
    }
}

另外一种就是固定一个数,调用取三个数的算法。

public List<List<Integer>> fourSum1(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            int check = target - nums[i];
            if (i > 0 && nums[i - 1] == nums[i])
                continue;
            List<List<Integer>> getThreeSums = threeSum(nums, i + 1,
                    nums.length - 1, check);
            for (List<Integer> subList : getThreeSums) {
                subList.add(nums[i]);
                result.add(subList);
            }
        }
        return result;
    }

    public List<List<Integer>> threeSum(int[] nums, int begin, int finish,
            int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        for (int i = begin; i <= finish; i++) {
            if (i > begin && nums[i - 1] == nums[i])
                continue;
            int start = i + 1;
            int end = finish;
            int check = target - nums[i];
            while (start < end) {
                int twoSum = nums[start] + nums[end];
                if (twoSum > check)
                    end--;
                else if (twoSum < check)
                    start++;
                else {
                    List<Integer> subResult = new ArrayList<Integer>();
                    subResult.add(nums[i]);
                    subResult.add(nums[start++]);
                    subResult.add(nums[end--]);
                    result.add(subResult);
                    while (start < end && nums[start] == nums[start - 1])
                        start++;
                    while (start < end && nums[end] == nums[end + 1])
                        end--;
                }
            }
        }
        return result;
    }
目录
相关文章
|
算法
light oj 1255 - Substring Frequency (KMP)
输入两个字符串,计算二串在一串中出现的次数。 裸裸的KMP,参考刘汝佳《算法竞赛入门经典训练指南》 P212 或数据结构。
40 1
[LeetCode]--69. Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. 我采用的是二分法。每次折中求平方,如果大了就把中值赋给大的,如果小了就把中值赋给小的。 public int mySqrt(int x) { long start = 1, end = x; while
865 0
[LeetCode]--71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, =&gt; “/home” path = “/a/./b/../../c/”, =&gt; “/c” click to show corner cases. Corner Cases:
1074 0
|
机器学习/深度学习
[LeetCode]--50. Pow(x, n)
Implement pow(x, n). 其实这个题递归和非递归应该都不难实现,就是边界值的问题。 public double myPow(double x, int n) { if (n == 0) return 1.0; double res = 1.0; if (n &lt; 0) {
1019 0
|
算法
[LeetCode]--47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1],
1064 0
[LeetCode]--15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not
1266 0
[LeetCode]--5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 我用的是中心扩展法 因为
1240 0
[LeetCode]--389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter th
1232 0
[LeetCode]--299. Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide
1158 0