LeetCode 18. 4Sum new

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

这道题是要求找到4个数之和等于给定的数字,解答方法和xxxx很像,但是复杂度要更高一些,而且这道题还有一个难点就是需要去除重复的解,这一点做的不好会导致无法AC测试用例,解答方法仍然是先排序,然后固定2个数字,用双指针遍历剩余的所有数字,4个数字之和等于target,则把这4个数作为一组解保存到结果里,4个数每一个都需要做一些措施防止重复解答

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

下面的解法很巧,利用的set元素的无重复性解答

 1 // O(n^3)
 2 class Solution {
 3 public:
 4     vector<vector<int> > fourSum(vector<int> &nums, int target) {
 5         set<vector<int> > res;
 6         sort(nums.begin(), nums.end());
 7         for (int i = 0; i < int(nums.size() - 3); ++i) {
 8             for (int j = i + 1; j < int(nums.size() - 2); ++j) {
 9                 int left = j + 1, right = nums.size() - 1;
10                 while (left < right) {
11                     int sum = nums[i] + nums[j] + nums[left] + nums[right];
12                     if (sum == target) {
13                         vector<int> out;
14                         out.push_back(nums[i]);
15                         out.push_back(nums[j]);
16                         out.push_back(nums[left]);
17                         out.push_back(nums[right]);
18                         res.insert(out);
19                         ++left; --right;
20                     } else if (sum < target) ++left;
21                     else --right;
22                 }
23             }
24         }
25         return vector<vector<int> > (res.begin(), res.end());
26     }
27 };

原文地址:https://www.cnblogs.com/dapeng-bupt/p/8331648.html

时间: 2024-10-17 08:21:00

LeetCode 18. 4Sum new的相关文章

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

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

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

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