18. 4Sum

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

分析

两层循环,然后按照3Sum的做。O(n3)的复杂度。150ms


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

class Solution {

public:

    vector<vector<int>> fourSum(vector<int>& nums, int target) {

        int len = nums.size();

        vector<vector<int> > result;

        if(len < 4) return result;

        sort(nums.begin(), nums.end());

        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 l = j + 1;

                int r = len - 1;

                while( l < r){

                    if( nums[i] + nums[j] + nums[l] + nums[r] == target){

                        result.push_back(vector<int>{nums[i], nums[j], nums[l], nums[r]});

                        l++;

                        while( l < r && nums[l] == nums[l - 1])

                            l++;

                    }

                    else if( nums[i] + nums[j] + nums[l] + nums[r] < target){

                        l++;

                    }

                    else{

                        r--;

                    }

                }

            }

        }

        return result;

    }

};

进一步优化:https://discuss.leetcode.com/topic/37878/c-implementation-with-carefully-pruning-accelerates-a-lot-from-100ms-to-16ms

对排序后的数组,进行计算的时候,如果满足

1. 最靠前的4个数字之和 > target,则退出计算,因为以后也一定不会满足

2. 前面的数组和 末尾的数字之和 < target,那么说明肯定中间数字之和一定是 < target的,继续


1

2

if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;

if(nums[i]+nums[len-3]+nums[len-2]+nums[len-1]<target) continue;

同时优化满足条件的判断顺序以及后续处理,可以得到如下代码:16ms


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

class Solution {

public:

    vector<vector<int>> fourSum(vector<int>& nums, int target) {

        int len = nums.size();

        vector<vector<int> > result;

        if(len < 4) return result;

        sort(nums.begin(), nums.end());

        for(int i = 0; i < len - 3; ++i){

            if( i > 0 && nums[i] == nums[i - 1]){

                continue;

            }

            /** cut edge to accelerate the speed **/

            if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;

            if(nums[i]+nums[len-3]+nums[len-2]+nums[len-1]<target) continue;

            for(int j = i + 1; j < len - 2; ++j){

                if(j > i + 1 && nums[j] == nums[j - 1]){

                    continue;

                }

                /** cut edge to accelerate the speed **/

                if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;

                if(nums[i]+nums[j]+nums[len-2]+nums[len-1]<target) continue;

                int l = j + 1;

                int r = len - 1;

                while( l < r){

                    int sum = nums[i] + nums[j] + nums[l] + nums[r];

                    if( sum < target){

                        l++;

                    }

                    else if( sum > target){

                        r--;

                    }

                    else{

                        result.push_back(vector<int>{nums[i], nums[j], nums[l], nums[r]});

                        /** cut edge to accelerate the speed **/

                        r--;l++;

                        while( l < r && nums[l] == nums[l - 1])l++;

                        while( l < r && nums[r] == nums[r + 1])r--;

                    }

                }

            }

        }

        return result;

    }

};

来自为知笔记(Wiz)

时间: 2024-08-06 07:55:53

18. 4Sum的相关文章

1. Two Sum&amp;&amp;15. 3Sum&amp;&amp;18. 4Sum

题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Given nums = [2, 7, 1

18. 4Sum(js)

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. Note: The solution set must not cont

[LeetCode][Python]18: 4Sum

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 18: 4Sumhttps://oj.leetcode.com/problems/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

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

题目描述: 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. 解题分析: 这个和3Sum那道题有些像,也是要先确定两个数,之后两个数的确定可以参考二分查找的实现方法进行优化. 具体代码: 1 pu

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

18 4Sum((寻找四个数之和为指定数的集合Medium))

题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要判断重复 图书馆的网,已经到了令人发指的程度,我告诫自己千万不要暴躁. 1 class Solution { 2 public: 3 vector<vector<int>> fourSum(vector<int>& nums, int target) { 4 vec

LeetCode:18. 4Sum(Medium)

1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个数之和等于目标整数target.请找出所有满足此条件的四个整数. 3. 解题思路 先对nums进行排序,然后采用两层for循环来确定前两个数字,最后在第二层for循环中确定后两个数字. 注意可能存在重复解!! 如下图所示,对Input先进行排序:[-4, -1, -1,0, 1,2],target