题目: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. Poisson Image Editing

    说起泊松,可以顺便提及一下泊松同学的老师,拉普拉斯.学图像或是信号的,一定对拉普拉斯算子和拉普拉斯卷积很熟悉.在泊松图像融合出现之前,也有一种叫Laplacian pyramid blending的融 ...

  2. 在Xcode中想要清屏该怎么实现

    XCODE的控制台不会有清屏效果,并没有像终端一样可以clear.但在某些时候我们非得想要清屏该怎么办呢??你去打开可执行文件,就会有类似清屏的效果.实际上是它帮你自动换页了,xcode左边是可以看到 ...

  3. MySQL5.7中新增的JSON类型的使用方法

    创建表json_test: CREATE TABLE json_test(id INT(11) AUTO_INCREMENT PRIMARY KEY,person_desc JSON)ENGINE I ...

  4. 智能车学习(十六)——CCD学习

    一.使用硬件 1.兰宙CCD四代      优点:可以调节运放来改变放大倍数      缺点:使用软排线(容易坏),CCD容易起灰,需要多次调节   2.野火K60底层     二.CCD硬件电路 ( ...

  5. struts2框架快速入门小案例

    struts2快速入门: index.jsp------>HelloAction--------->hello.jsp struts2流程 1.导入jar包 struts2的目录结构: a ...

  6. 【bootstrapValidator 不验证】使用bootstrapValidator 验证效果不起作用

    虽然在页面ready的时候 就绑定了验证表单 ,但是在点击提交按钮之后 依旧没有验证的效果 . 那就在提交按钮的点击事件中 添加一句话: $(document).ready( function () ...

  7. ArcGIS 点到直线的距离

    /****点到直线的距离*** * 过点(x1,y1)和点(x2,y2)的直线方程为:KX -Y + (x2y1 - x1y2)/(x2-x1) = 0 * 设直线斜率为K = (y2-y1)/(x2 ...

  8. HowTo:使用数据流读写消息

      本文主要演示使用TPL 数据流库从数据流块(dataflow block)读写消息. 提供了同步方法和异步方法. 主要使用BufferBlock,其既能作为message source,有能作为m ...

  9. H

    很爽的一局,打了70分钟,还刷新了我的最高击杀记录.打完出来一看居然是H局,第一次在H局里打出不错的表现诶.好像找人说一说,可惜并没有谁听,以前的朋友也不在了,还是算了,自己心里慢慢发霉去吧. 这局末 ...

  10. BZOJ4607 : [PA2015 Final]Edycja

    显然做完操作$2$后再做操作$1$. 建立一个$26$个点的有向图,每个点只有一条出边,$i$->$j$表示$i$最终变成了$j$,边权为一开始是$i$,最后不是$j$的位置个数,如果$i\ne ...