LeetCode18 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.(Medium)

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

分析:

还是采取跟2sum,3sum一样的思路,先排序,再two pointers.

实现的时候利用3sum,然后加一层循环,复杂度O(n^3).

代码:

 1 class Solution {
 2 private:
 3     vector<vector<int>> threeSum(vector<int>& nums, int target) {
 4         vector<vector<int>> v;
 5         if (nums.size() < 3) {  //数组元素个数过少,直接返回
 6             return v;
 7         }
 8         for (int i = 0; i < nums.size() - 2; ++i) {
 9             if (i >= 1 && nums[i] == nums[i - 1]) {
10                 continue;
11             }
12             int start = i + 1, end = nums.size() - 1;
13             while (start < end) {
14                 if ( nums[i] + nums[start] + nums[end] == target ) {
15                     vector<int> temp{nums[i], nums[start], nums[end]};
16                     v.push_back(temp);
17                     start++;
18                     end--;
19                     while ( (start < end) && nums[start] == nums[start - 1]) { //没加start < end虽然过了,估计是样例不够完善
20                         start++;
21                     }
22                     while ( (start < end) && nums[end] == nums[end + 1]) {
23                         end--;
24                     }
25                 }
26                 else if (nums[i] +  nums[start] + nums[end] > target ) {
27                     end--;
28                 }
29                 else {
30                     start++;
31                 }
32             }
33         }
34         return v;
35     }
36 public:
37     vector<vector<int>> fourSum(vector<int>& nums, int target) {
38         sort(nums.begin(), nums.end());
39         vector<vector<int>> v;
40         for (int i = 0; i < nums.size(); ++i) {
41             if (i > 0 && nums[i] == nums[i - 1]) {
42                 continue;
43             }
44             vector<int> temp(nums.begin() + i + 1, nums.end());
45             vector<vector<int>> result = threeSum(temp, target - nums[i]);
46             for (int j = 0; j < result.size(); ++j) {
47                 result[j].push_back(nums[i]);
48                 v.push_back(result[j]);
49             }
50         }
51         return v;
52     }
53 };
时间: 2024-12-30 09:32:07

LeetCode18 4Sum的相关文章

简单算法汇总

一.全排列问题(Permutation) 问题描写叙述:即给定{1,2,3},返回123,132,213,231,312,321 <Permutation> 1)无顺序的全排列问题: 将序列P(n) = {1-.. n}的全排列问题看成P(n)={1,P(n-1)} + {2,P(n-1)}-..的问题.即确定第一个元素的值为1.然后和剩下n-1个元素的全排列结果组合到一起:然后再将1和剩下的每一个元素进行交换.然后和其剩下的n-1个元素排列结果进行组合.显然这是一个递归问题. // 递归实现

LeetCode18: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 order.

[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

[Leetcode] 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 order.

LeetCode 017 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 454. 4Sum II

454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18801 Difficulty: Medium Contributors: Samuri Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[

[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

18 &amp; 454. 4Sum I &amp; II

18. 4Sum I (32lines) Given a integer array and target, find all unique quadruplets sum is target. Solution(O(N^3)): Similar as 3sum problem, add one more loop and be careful about the duplicates. 1 public class Solution { 2 public List<List<Integer&

[leetcode]4Sum @ Python

原题地址:http://oj.leetcode.com/problems/4sum/ 题意:从数组中找到4个数,使它们的和为target.要求去重,可能有多组解,需要都找出来. 解题思路:一开始想要像3Sum那样去解题,时间复杂度为O(N^3),可无论怎么写都是Time Limited Exceeded.而同样的算法使用C++是可以通过的.说明Python的执行速度比C++慢很多.还说明了一点,大概出题人的意思不是要让我们去像3Sum那样去解题,否则这道题就出的没有意义了.这里参考了kitt的解