LeetCode 18. 四数之和(4Sum)

题目描述

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

解题思路

借鉴三数之和的思路,首先将数组从大到小排序,然后从数组首位置开始遍历:

  • 固定第一个数,将其加入结果集合,为了避免重复,若该数与前一个数相同则跳过
  • 接着固定第二个数为第一个数的下一个位置,将其加入结果集合,同样为了避免重复,若该数与前一个数相同则跳过
  • 用双指针法,左右指针分别指向第二个数的下一个位置和最后一个位置,算出两数的目标和,若两位置的数之和正好等于目标,则加入到结果集合中,并将左指针指向右边第一个与当前数不相同的位置,同时右指针向左移动一位;若两位置的数之和小于目标,则将左指针向右移动一位;若两位置的数之和大于目标,则将右指针向左移动一位。这样移动直到左指针超过右指针或与其相遇
  • 根据回溯法的思想,遍历完第一个数和第二个数的解之后,分别将其弹出结果集合

代码

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

原文地址:https://www.cnblogs.com/wmx24/p/9205532.html

时间: 2024-08-27 18:13:16

LeetCode 18. 四数之和(4Sum)的相关文章

[Leetcode 18]四数之和 4 Sum

[题目] 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 d

LeetCode 18. 四数之和

题意 从数组中找出满足和为target的四元组. 思路 想法1:暴力,\(O(n^4)\). 想法2:排序 + 指针.思路类似前面的三数之和,只不过这里的指针多了一个,后两个指针相遇时第二个指针后移一个单位.时间复杂度:\(O(n^3)\),空间复杂度:\(O(1)\). 代码 class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { int len

LeetCode:四数之和【18】

LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为:[ [-1, 0, 0, 1], [-2, -1, 1, 2]

[Swift]LeetCode18. 四数之和 | 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 contain dupli

18. 四数之和

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为:[ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2]] /* 解题思

[LeetCode] 454. 4Sum II 四数之和II

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the r

Leetcode(18)-四数之和

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ] 思路:

LeetCode第十八题-四数之和

4Sum 问题简介:定n个整数和整数目标的数组nums,是否有元素a,b,c,d在nums中,使a+b+c+d=target? 举例: 给定数组 nums = [1, 0, -1, 0, -2, 2], 目标值 target = 0. 结果: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ] 解法一:先将数组排序,通过三层循环,寻找是否符合四数之和的结果 注: 1.contains方法使用场景 list/Set - contains() Map -

两数之和,三数之和,最接近的三数之和,四数之和

LeetCode有一系列做法套路相同的题目,N数之和就可以算一个 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 第一个解决办法,简单暴力,堆for循环就是,但