题目: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:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.

给定一个数组,找出所有4个数的组合使得它们的和为target

思路:和3Sum,Two Sum都是类似的。主要是两个点:

1. 对数组排序,然后双指针分别从头尾遍历

2. 注意去重的问题

 class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int nsize = nums.size();
sort(nums.begin(), nums.end());
vector<vector<int>> result;
for(int i = ; i < nsize; i++){
if(i != && nums[i] == nums[i - ])
continue;
for(int j = i + ; j < nsize; j++){
if(j != i + && nums[j] == nums[j - ])
continue;
int p = j + , q = nsize - ;
while(p < q){
int sum = nums[i] + nums[j] + nums[p] + nums[q];
if(sum < target){
++p;
continue;
}
if(sum > target){
--q;
continue;
} vector<int> tmp;
tmp.push_back(nums[i]);
tmp.push_back(nums[j]);
tmp.push_back(nums[p]);
tmp.push_back(nums[q]);
result.push_back(tmp); while(++p < q && nums[p] == nums[p - ]);
while(p < --q && nums[q] == nums[q + ]);
}
}
}
return result;
}
};

【LeetCode】16. 4Sum的更多相关文章

  1. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  2. 【LeetCode】18. 4Sum (2 solutions)

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

  3. 【LeetCode】018 4Sum

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

  4. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  5. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  6. 【LeetCode】18. 4Sum

    题目: 思路:这题和15题很像,外层再加一个循环稍作修改即可 public class Solution { public List<List<Integer>> fourSu ...

  7. 【LeetCode】16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  8. 【LeetCode】454 4Sum II

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

  9. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

随机推荐

  1. 第二十四篇:导出SOUI对象到LUA脚本

    LUA是一种体积小,速度快的脚本语言.脚本语言虽然性能上和C++这样的Naitive语言相比差一点,但是开发速度快,可以方便的更新代码等,近年来受到了越来越多开发者的重视. 在SOUI框架中,我把脚本 ...

  2. 在Salesforce中创建Schedule的Job去定时触发对应的Class

    在Salesforce中也存在着Job的概念,当然了我们可以创建Schedule的Job去定时触发对应的Class,来完成我们所需要定时处理的功能,比如:定时同步数据. 具体处理步骤如下所示: 1): ...

  3. OD附加功能分析

    OD版本:OllyICE v1.10   在从文件菜单选择附加后,OD会在注册一个窗口类后,先创建一个0x138大小的进程表; 再是CreateWindowExA 创建窗口;   00478013 l ...

  4. SQLServer 维护脚本分享(05)内存(Memory)

    --查看设置的最大最小每次 exec sp_configure 'max server memory (MB)' exec sp_configure 'min server memory (MB)' ...

  5. 读懂Android项目结构目录

    我们看到下图:当我们创建了第一Android项目的时候有没有被吓到.怎么这么多目录,好头晕啊!没事, 那我们今天就了解一下这些目录是做什么的: src: src 目录是放置我们所有 Java 代码的地 ...

  6. css3 -- 背景图处理

    1.多背景图片: p{ background-image:url() , url(); background-position:95% 90% , 50% 50%; background-repect ...

  7. Javascript参考手册

    ---------------------------------------------------------------------------------------------------- ...

  8. 同步、更新、下载Android Source & SDK from 国内镜像站

    转自: 同步.更新.下载Android Source & SDK from 国内镜像站 Download the android source from china mirrors     以 ...

  9. 把Actor绑定到角色的插槽上

    void AMonster::PostInitializeComponents(){ Super::PostInitializeComponents(); // instantiate the mel ...

  10. HDU4307 Matrix(最小割)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4307 Description Let A be a 1*N matrix, and each ...