1. Question

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

]

2. Solution

这个问题的解法和3sum一样,只不过现在是4个数求和了,先遍历所有两个数字的组合,然后在去找另外两个数,时间复杂度为O(n^3)。

3. Code

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
if (nums.size() <= 3)
return vector<vector<int>>();
sort(nums.begin(), nums.end()); vector<vector<int>> res;
for (int i = 0; i < nums.size() - 3; i++) {
for (int j = i + 1; j < nums.size() - 2; j++) {
int start = j + 1;
int end = nums.size() - 1;
int tmp_target = target - (nums[i] + nums[j]);
while (start < end) {
int tmp = nums[start] + nums[end];
if (tmp > tmp_target)
end--;
else if (tmp < tmp_target)
start++;
else {
vector<int> result;
result.push_back(nums[i]);
result.push_back(nums[j]);
result.push_back(nums[start]);
result.push_back(nums[end]);
res.push_back(result); // 去掉start开始重复的
while (start < end && nums[start] == result[2])
start++;
// 去掉end开始重复的
while (end > start && nums[end] == result[3])
end--;
}
}
// 去掉重复的第二个数
while (j + 1 < nums.size() - 2 && nums[j + 1] == nums[j])
j++;
}
// 去掉重复的第一个数
while (i + 1 < nums.size() - 3 && nums[i + 1] == nums[i])
i++;
}
return res;
}
};

LeetCode——4Sum的更多相关文章

  1. LeetCode——4Sum &amp; 总结

    LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...

  2. [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 ...

  3. [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 ...

  4. leetcode — 4sum

    import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...

  5. LeetCode 4Sum 4个数之和

    题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...

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

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

  8. leetcode 4sum python

    class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :t ...

  9. LeetCode 4Sum (Two pointers)

    题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

随机推荐

  1. Android中可自由移动悬浮窗口的实现

    http://www.xsmile.net/?p=538   调用WindowManager,并设置WindowManager.LayoutParams的相关属性,通过WindowManager的ad ...

  2. cross-compler toolchains--clfs

    http://www.cnblogs.com/leaven/archive/2010/11/17/1879679.html

  3. Mysql2索引

    索引分类: 作用:优化查询,select查询有三种情况:缓存查询(不在mysql中进行数据查询),全表查询,索引扫描 Btree(btree b+tree b*tree) Rtree HASH Ful ...

  4. getApplicationContext()、Activity.this、 getBaseContext区别

    getApplicationContext()返回应用的上下文,生命周期是整个应用,应用退出它才被摧毁 Activity.this 返回当前activity的上下文,属于activity ,activ ...

  5. 模块讲解----sys

    sys:跟python解释器相关的信息 #命令行参数list,第一个元素时程序本身路径 print(sys.argv) 注意:执行脚本时,可以传参数. #退出程序,正常退出时exit(0) sys.e ...

  6. 数据结构-平衡二叉树 旋转过程平衡因子分析 c和java代码实现对比

    平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且 ...

  7. css 自定义滚动条

    我遇到的场景: 对于iframe窗口,自带滚动条是整个窗口的大小.有时需要顶部或底部固定,则滚动条不应该触碰到顶部或底部. 那么首先打开iframe时应该去掉滚动条 scrolling="n ...

  8. PHP压缩与解压Zip(PHPZip类)

    <?php     class PHPZip     {         private $ctrl_dir     = array();         private $datasec    ...

  9. Element 中表单非必填数据项 必须为数字的验证问题

    Element-ui 的el-form组建中,自带基本的验证功能,比如某些项必填的验证,直接加入rules 规则中即可,如下实例: 在页面中书写如下: <el-form-item label=& ...

  10. 160726 smarty 笔记(2)

    <?php //取当前页 $p=1; if(!empty($_GET["page"])) { $p=$_GET["page"]; } //定义页面缓存文件 ...