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. (ie, abcd)
  • 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)

LeetCode 中关于数字之和还有其他几道,分别是 Two Sum ,3Sum ,3Sum Closest,虽然难度在递增,但是整体的套路都是一样的,在这里为了避免重复项,我们使用了 STL 中的 TreeSet,其特点是不能有重复,如果新加入的数在 TreeSet 中原本就存在的话,插入操作就会失败,这样能很好的避免的重复项的存在。此题的 O(n^3) 解法的思路跟 3Sum 基本没啥区别,就是多加了一层 for 循环,其他的都一样,代码如下:

解法一:

class Solution {
public:
vector<vector<int>> fourSum(vector<int> &nums, int target) {
set<vector<int>> res;
sort(nums.begin(), nums.end());
for (int i = ; i < int(nums.size() - ); ++i) {
for (int j = i + ; j < int(nums.size() - ); ++j) {
if (j > i + && nums[j] == nums[j - ]) continue;
int left = j + , right = nums.size() - ;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
vector<int> out{nums[i], nums[j], nums[left], nums[right]};
res.insert(out);
++left; --right;
} else if (sum < target) ++left;
else --right;
}
}
}
return vector<vector<int>>(res.begin(), res.end());
}
};

但是毕竟用 TreeSet 来进行去重复的处理还是有些取巧,可能在 Java 中就不能这么做,那么还是来看一种比较正统的做法吧,手动进行去重复处理。主要可以进行的有三个地方,首先在两个 for 循环下可以各放一个,因为一旦当前的数字跟上面处理过的数字相同了,那么找下来肯定还是重复的。之后就是当 sum 等于 target 的时候了,在将四个数字加入结果 res 之后,left 和 right 都需要去重复处理,分别像各自的方面遍历即可,参见代码如下:

解法二:

class Solution {
public:
vector<vector<int>> fourSum(vector<int> &nums, int target) {
vector<vector<int>> res;
int n = nums.size();
sort(nums.begin(), nums.end());
for (int i = ; i < n - ; ++i) {
if (i > && nums[i] == nums[i - ]) continue;
for (int j = i + ; j < n - ; ++j) {
if (j > i + && nums[j] == nums[j - ]) continue;
int left = j + , right = n - ;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
vector<int> out{nums[i], nums[j], nums[left], nums[right]};
res.push_back(out);
while (left < right && nums[left] == nums[left + ]) ++left;
while (left < right && nums[right] == nums[right - ]) --right;
++left; --right;
} else if (sum < target) ++left;
else --right;
}
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/18

类似题目:

Two Sum

3Sum

4Sum II

参考资料:

https://leetcode.com/problems/4sum/

https://leetcode.com/problems/4sum/discuss/8549/My-16ms-c%2B%2B-code

https://leetcode.com/problems/4sum/discuss/8575/Clean-accepted-java-O(n3)-solution-based-on-3sum

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. shell 计算
  2. node.js+socket.io安装
  3. 什么是viewport,为什么需要viewport
  4. Redis_持久化之RDB
  5. EXT Grid celleditor列编辑,动态控制某一单元格只读
  6. java基础学习总结——this关键字
  7. 解决Android开发中,ActiveAndroid和Gson同时使用,对象序列化失败的问题
  8. 【转】NSDictionary以及NSMutableDictionary的用法
  9. Unity3d Fast Indirect illumination Using Two Virtual Spherical Gaussian Lights-Square Enix论文
  10. C++ STL之map常用指令
  11. python多进程断点续传分片下载器
  12. vs2013+opencv2.4.11+Qt5.5.1配置
  13. 为什么还坚持.NET? 找一门适合自己的语言去做编程
  14. Protobuf-net判断字段是否有值
  15. 多进程log4cxx区分日志
  16. 04flask_scripts使用
  17. mysql 快速生成删除数据库中所有的表的语句
  18. php通过CURL模拟post提交请求
  19. HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】
  20. django内置分页功能扩展

热门文章

  1. 2.C#面向对象基础属性
  2. Notepad++ 实用技巧
  3. [原创]django+ldap实现单点登录(装饰器和缓存)
  4. IIS服务器多域名证书绑定443端口解决方案
  5. .NET 实现并行的几种方式(二)
  6. C#-#define条件编译
  7. C#向sql server数据表添加数据源代码
  8. 【转载】C#怎么判断字符是不是汉字
  9. PHP中this,self,parent三个关键字
  10. Android中的Libraries以及Order and Export的使用。