码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 18. 4Sum new

时间:2018-01-23 01:04:21      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:ret   一点   nts   去除   log   这一   复杂度   span   insert   

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 };

 

LeetCode 18. 4Sum new

标签:ret   一点   nts   去除   log   这一   复杂度   span   insert   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!