Array + two points leetcode.18 - 4Sum

题面

Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + bc + d = target? Find all unique quadruplets in the array which gives the sum of target.

给定无序数组,找到和为target的不重复长度为4的子序列

样例

1. Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]
2. Given array nums = [0, 0, 0, 0], and target = 0.
solution set is:
[
  [0, 0, 0, 0]
]note: This example need to think specially!
3. Given array nums = [-3, -3, -3, 2, 2, 2,0, 0, 0, 3, 3, 3], and target = 0.
solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]Note: How to deal with the duplicate quadruplets ?

思路(这里是4个数的和为target,我们之前不是做过3个数的吗?Refer this one 正好可以用上)

1. 升序排序(sort即可)

2. 不可避免地要遍历数组(i)

3. 借鉴leetcode-15中的三个数的和,我们如法炮制,搜索剩下的三个数(j = i+1, l=j+1, r = size()-1);对参数有疑惑的话,看下面代码。

时间复杂度:O(n3),暴力的话应该会超时,没试!

这题里面尤其要注意避免重复

源码

 1 class Solution {
 2 public:
 3     vector<vector<int>> fourSum(vector<int>& nums, int target) {
 4         vector<vector<int>> res;
 5         int len = nums.size();
 6         if(len < 4)
 7             return res;
 8
 9         sort(nums.begin(), nums.end());
10         for(int i=0; i<=len-4; i++)
11         {
12             while(i>0 && i<=len-4 && nums[i]==nums[i-1])
13                 i++;//消除i的重复
14             for(int j=i+1; j<=len-3; j++)
15             {
16                 while(j>i+1 && j<=len-3 && nums[j]==nums[j-1])
17                     j++;//消除j的重复
18                 int l=j+1, r=len-1;
19                 while(l < r)
20                 {
21                     int sum = nums[i] + nums[j] + nums[l] + nums[r];
22                     if(sum > target)
23                         r--;
24                     else if(sum < target)
25                         l++;
26                     else
27                     {
28                         res.push_back(vector<int> {nums[i], nums[j], nums[l], nums[r]});
29                         while(l<r && nums[l]==nums[l+1]) l++;//消除l的重复
30                         while(l<r && nums[r]==nums[r-1]) r--;//消除r的重复
31                         l++; r--;
32                     }
33                 }
34             }
35         }
36         return res;
37     }
38 };

如果对消除重复有疑问的童鞋,请留言, 或者自行把example 3 手推一遍就明白了。

原文地址:https://www.cnblogs.com/yocichen/p/10859278.html

时间: 2024-10-24 11:14:28

Array + two points 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(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

LeetCode——18. 4Sum

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

Array + two points leetcode.16 - 3Sum Closest

题面 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 给定数组,找出并返回最接近target的

[leetcode 18]4Sum

1 题目: 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

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