4 Sum

作者: Herbert_Ti | 来源:发表于2017-08-18 11:28 被阅读0次
题目

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.

给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d)。

https://leetcode.com/problems/4sum/description/

样例

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

例如,对于给定的整数数组S=[1, 0, -1, 0, -2, 2] 和 target=0. 满足要求的四元组集合为:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)

解题思路

针对4 Sum这个题,我的思路还是两个指针 Two Pointers,时间复杂度是O(n^3)。
针对这个题,有这么一点是要特别注意的,就是 The solution set must not contain duplicate quadruplets.,我们要确保每一组答案都不包含数组中原先已经存在的元素。
实现过程是,从当前元素i开始,寻找下一个元素i+1,再从i+1之后开始双指针,如果结果小于目标值,start增加;如果结果大于目标值,end减小。当等于目标值的时候,输出结果。
具体代码如下:

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

相关文章

  • two sum&&three sum&&four sum&&th

    two sum 3 sum 3Sum Closest 4 sum 利用set来实现: 3 sum 4 sum

  • PowerBI DAX CALCULATE/SUM/COUNT/

    SUM CALCULATE(SUM...) COUNT 4.COUNTROWS COUNTBLANK 计算空值的个数

  • 二维数组

    sum()的函数原型:int sum( int (*ar2)[4], int size );//传递一个指向由 4...

  • 写一个sum函数使得以下表达式的值正确

    sum(1,2,3).sumOf();//6sum(2,3)(2).sumOf();//7sum(1,2,3,4)...

  • 4 sum

    4 sum Given an array nums of n integers and an integer ta...

  • 4 sum

    solution 1: sort the array, then fix two,using two pointe...

  • 4 Sum

    题目 Given an array S of n integers, are there elements a, ...

  • 4 sum

    解题报告, 这个做的比较绝望, 用了two points, recursion, memorization= =,...

  • 15+18、3Sum 4Sum

    15、3Sum Example 复杂度 思路 解法 18、4Sum Example 解法

  • Day-13求和函数

    今天学习了SUM SUMIF SUMIFS的用法 1、连续区域求和=SUM(B4:B8)这里输入SUM函数选取求...

网友评论

      本文标题:4 Sum

      本文链接:https://www.haomeiwen.com/subject/nyvorxtx.html