LeetCode 18. 4Sum

原创 2018年04月15日 15:33:31

问题描述

  • 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.
  • Note :
    The solution set must not contain duplicate quadruplets.
  • Example

Given array nums = [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]
]

问题分析

经验教训

  • 由 2Sum, 3Sum, 4Sum 推导出 k Sum

代码实现

    public List<List<Integer>> fourSum(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        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 left = j + 1;
                int right = nums.length - 1;
                int sum = target - nums[i] - nums[j];
                while (left < right) {
                    if (nums[left] + nums[right] == sum) {
                        ArrayList<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[left++]);
                        list.add(nums[right--]);
                        res.add(list);
                        while (left < right && nums[left] == nums[left - 1]) {
                            ++left;
                        }
                        while (left < right && nums[right] == nums[right + 1]) {
                            --right;
                        }
                    }else if (nums[left] + nums[right] < sum) {
                        ++left;
                    }else {
                        --right;
                    }
                }
            }
        }
        return res;
    }
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79949745

(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 = t...
  • u012848330
  • u012848330
  • 2016-05-30 19:59:46
  • 982

LeetCode 18 4Sum (C,C++,Java,Python)

Problem: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b...
  • runningtortoises
  • runningtortoises
  • 2015-05-10 15:04:02
  • 1464

leetcode 18 -- 4Sum

4Sum 题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c...
  • wwh578867817
  • wwh578867817
  • 2015-06-04 15:12:14
  • 1118

18. 4Sum Leetcode Python

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-17 11:55:43
  • 916

leetcode 18. 4Sum KSum的解决办法

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar...
  • JackZhang_123
  • JackZhang_123
  • 2017-08-31 15:50:38
  • 183

leetcode解题之 18. 4Sum Java版(结果是目标值的四个数字和)

leetcode解题之 18. 4Sum Java版(结果是目标值的四个数字和)
  • mine_song
  • mine_song
  • 2017-04-06 18:43:11
  • 230

[C++]LeetCode: 71 4Sum && kSum总结

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
  • cinderella_niu
  • cinderella_niu
  • 2015-01-06 10:43:37
  • 1856

leetcode-Java-18. 4Sum

public class Solution { public List fourSum(int[] nums, int target) { List list = new Ar...
  • github_34514750
  • github_34514750
  • 2016-06-03 18:18:09
  • 422

[leetcode: Python]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 = tar...
  • NXHYD
  • NXHYD
  • 2017-05-19 08:18:55
  • 330

【LeetCode-面试算法经典-Java实现】【018-4Sum(四个数的和)】

【018-4Sum(四个数的和)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given an array S of n integers, are there ele...
  • DERRANTCM
  • DERRANTCM
  • 2015-07-22 07:22:02
  • 2768
收藏助手
不良信息举报
您举报文章:LeetCode 18. 4Sum
举报原因:
原因补充:

(最多只允许输入30个字)