题意:

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).

代码:

 class Solution {
private:
vector<vector<int>> threeSum(vector<int>& nums, int target) {
vector<vector<int>> v;
if (nums.size() < ) { //数组元素个数过少,直接返回
return v;
}
for (int i = ; i < nums.size() - ; ++i) {
if (i >= && nums[i] == nums[i - ]) {
continue;
}
int start = i + , end = nums.size() - ;
while (start < end) {
if ( nums[i] + nums[start] + nums[end] == target ) {
vector<int> temp{nums[i], nums[start], nums[end]};
v.push_back(temp);
start++;
end--;
while ( (start < end) && nums[start] == nums[start - ]) { //没加start < end虽然过了,估计是样例不够完善
start++;
}
while ( (start < end) && nums[end] == nums[end + ]) {
end--;
}
}
else if (nums[i] + nums[start] + nums[end] > target ) {
end--;
}
else {
start++;
}
}
}
return v;
}
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
vector<vector<int>> v;
for (int i = ; i < nums.size(); ++i) {
if (i > && nums[i] == nums[i - ]) {
continue;
}
vector<int> temp(nums.begin() + i + , nums.end());
vector<vector<int>> result = threeSum(temp, target - nums[i]);
for (int j = ; j < result.size(); ++j) {
result[j].push_back(nums[i]);
v.push_back(result[j]);
}
}
return v;
}
};

LeetCode18 4Sum的更多相关文章

  1. [array] leetCode-18. 4Sum -Medium

    18. 4Sum -Medium descrition Given an array S of n integers, are there elements a, b, c, and d in S s ...

  2. leetcode18—4Sum

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  3. ARTS第八周

    1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有观点和思考的技术文章 以下是 ...

  4. 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 = tar ...

  5. [Swift]LeetCode18. 四数之和 | 4Sum

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  6. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  7. [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 = tar ...

  8. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  9. 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum

    18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c  ...

随机推荐

  1. Spark相比Hadoop MapReduce的特点

    (1)中间结果输出     基于MapReduce的计算引擎通常会将中间结果输出到磁盘上,进行存储和容错. 出于任务管道承接的考虑,当一些查询翻译到MapReduce任务时,往往会产生多个Stage, ...

  2. SCP和SFTP(转)

    原文:http://www.cnblogs.com/wang_yb/p/3819441.html 不管SCP还是SFTP,都是SSH的功能之一.都是使用SSH协议来传输文件的. 不用说文件内容,就是登 ...

  3. ACM 数论小结 2014-08-27 20:36 43人阅读 评论(0) 收藏

    断断续续的学习数论已经有一段时间了,学得也很杂,现在进行一些简单的回顾和总结. 学过的东西不能忘啊... 1.本原勾股数: 概念:一个三元组(a,b,c),其中a,b,c没有公因数而且满足:a^2+b ...

  4. 解决ehcache的UpdateChecker问题

    问题描述 项目中用了ssh框架,每次启动tomcat的时候都特别慢,会在这样一句话下面停留很久 [2016-01-08 23:55:51,517 INFO UpdateChecker.java:doC ...

  5. UVALive 7147 World Cup

    https://icpcarchive.ecs.baylor.edu/index.phpoption=com_onlinejudge&Itemid=8&page=show_proble ...

  6. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  7. POJ 2886Who Gets the Most Candies?(线段树)

    POJ 2886 题目大意是说有n个人围成一圈,游戏的起点是k,每个人持有一个数字(非编号)num,每次当前的人退出圈,下一个人是他左边的第num个(也就是说下一个退出的是k+num, k可以为负数, ...

  8. iis配置好后,解决打开服务器要输入用户名和密码的问题

    [转]IIS网站访问需要输入用户名和密码 xp系统下安装IIS5,并设置好网站路径,但是访问网站时需要输入用户名和密码,这个问题极大可能是因为你网站放置在一个文件系统为NTFS的盘符上,而IIS默认的 ...

  9. xiaocms 关于搜索功能 添加搜索字段

    自己折磨了好几天 就是没研究个出像样的的东西 看了一下 core/controller/index.php searchAction()方法 但是不知从何下手.查了sql语句,还是没实现 请教了一位自 ...

  10. OC:通讯录实战

    实战(使用OC的知识制作一个简易通讯录) //语法糖.笑笑语法 // NSString * string = [NSString stringWithFormat:@"string" ...