[leetcode 18]4Sum

1 题目:

Given an array S of n integers, are there elements abc, 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:

  • 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.
    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)

2 思路

首先,得理解3Sum。

然后按照3Sum的思路,就好做了。主要是要遍历所有的四个组合。排序是肯定的。详情见代码吧,自己想的。。时间复杂度o(n^3)做了几题,感觉有点状态了。

3 代码

    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        int len = nums.length;
        if(len < 4){
            return result;
        }

        Arrays.sort(nums);
        int head = 0;
        int end = len-1;
        while(head < end -2){
            while(head < end-2){
                int pre = head + 1;
                int suffix = end - 1;
                /*  compare all four group between pre & suffic   */
                while(pre < suffix){
                    int sum = nums[head]+nums[end]+nums[pre]+nums[suffix];
                    if(sum < target){
                        ++pre;
                    }else if(sum > target){
                        --suffix;
                    }else{
                        result.add(Arrays.asList(nums[head],nums[pre],nums[suffix],nums[end]));
                        ++pre;
                        --suffix;
                        while(pre <= suffix && nums[pre]==nums[pre-1]) ++pre;
                        while(pre <= suffix && nums[suffix]==nums[suffix+1]) --suffix;
                    }
                }
                /* for each nums[head], compore all nums[head] until head<end-2 */
                ++head;
                while(head<end-2 && nums[head]==nums[head-1]) ++head;
            }
            /* set head to 0 & end-1 ,so we can traverse all four groups */
            head=0;
            --end;
            while(head<end-2 &&nums[end] == nums[end+1]) --end;
        }

        return result;
    }
时间: 2024-11-10 13:08:16

[leetcode 18]4Sum的相关文章

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 solution set must not contain duplicate quadruplets. For exampl

[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 solution set must not contain duplicate quadruplets. For exampl

LeetCode——18. 4Sum

一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数的和等于target,找出所有的四元组,当然这些四元组不能有重复的. 三.题解: 这道题实质就是3sum的变形,关于4sum问题,已经在https://www.cnblogs.com/wangkundentisy/p/9079622.html这里说过了,无外乎最外面两层循环,最里面的循环使用哈希表或

LeetCode 18 4Sum(4个数的和)

翻译 给定一个有n个数字的数组S,在S中是否存在元素a,b,c和d的和恰好满足a + b + c + d = target. 找出数组中所有的不想等的这四个元素,其和等于target. 备注: 在(a,b,c,d)中的元素必须从小到大排列.(a ≤ b ≤ c ≤ d) 其结果必须不能够重复. 例如,给定S = {1 0 -1 0 -2 2},target = 0. 一个结果集为: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2) 原文 Given an ar

leetCode 18.4Sum (4数字和) 解题思路和方法

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: Elements in a quadruplet (a,b,c,d) must be in non-descending o

LeetCode 18. 4Sum new

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 exampl

Array + two points leetcode.18 - 4Sum

题面 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b+ c + d = target? Find all unique quadruplets in the array which gives the sum of target. 给定无序数组,找到和为target的不重复的长度为4的子序列 样例 1. Given

LeetCode #18 4Sum (M)

[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. Note: Elements in a quadruplet (a,b,c,d) must be in non-descend

Java [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: Elements in a quadruplet (a,b,c,d) must be in non-descending