58-四数之和

给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d)。

注意事项

四元组(a, b, c, d)中,需要满足a <= b <= c <= d

答案中不可以包含重复的四元组。

样例

例如,对于给定的整数数组S=[1, 0, -1, 0, -2, 2] 和 target=0. 满足要求的四元组集合为:

(-1, 0, 0, 1)

(-2, -1, 1, 2)

(-2, 0, 0, 2)

标签

哈希表 排序 数组 两根指针

思路

延续三数之和的思路,先固定前2个数,然后后两个数按三数之和的方式,寻找符合条件的数。

code

class Solution {
public:
/**
* @param numbers: Give an array numbersbers of n integer
* @param target: you need to find four elements that's sum of target
* @return: Find all unique quadruplets in the array which gives the sum of
* zero.
*/
vector<vector<int> > fourSum(vector<int> nums, int target) {
// write your code here
int size = nums.size();
if(size < 4) {
return vector<vector<int> >();
}
vector<vector<int> > result; int i = 0, j = 0, k = 0, l = 0;
sort(nums.begin(), nums.end()); for(k=0; k<size; k++) {
if(k>0 && nums[k]==nums[k-1]) {
continue;
} for(l=k+1; l<size; l++) {
if(l>k+1 && nums[l]==nums[l-1]) {
continue;
} for(i=l+1, j=size-1; i<j;) {
if(i>l+1 && nums[i]==nums[i-1]) {
i++;
continue;
}
if(j<size-1 && nums[j]==nums[j+1]) {
j--;
continue;
} int sum = nums[i] + nums[j] + nums[k] + nums[l]; if(target == sum) {
vector<int> temp;
temp.push_back(nums[k]);
temp.push_back(nums[l]);
temp.push_back(nums[i]);
temp.push_back(nums[j]);
result.push_back(temp);
i++;
j--;
}
else if(target < sum) {
j--;
}
else {
i++;
}
}
}
}
return result;
}
};

lincode-58-四数之和的更多相关文章

  1. LeetCode第十八题-四数之和

    4Sum 问题简介:定n个整数和整数目标的数组nums,是否有元素a,b,c,d在nums中,使a+b+c+d=target? 举例: 给定数组 nums = [1, 0, -1, 0, -2, 2] ...

  2. ACM_四数之和

    四数之和 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个不同的整数,判断能否从中选4次,4个数和刚好为m.数字可重复选取. ...

  3. LeetCode18. 四数之和

    LeetCode18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值 ...

  4. [LeetCode] 454. 4Sum II 四数之和II

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

  5. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  6. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  7. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

  8. LeetCode:两数之和、三数之和、四数之和

    LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...

  9. 【LeetCode】18.四数之和

    题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...

  10. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

随机推荐

  1. bzoj3895: 取石子(博弈论,记忆化搜索)

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 361  Solved: 177[Submit][Status][Discuss] D ...

  2. js中String 转化为 Date

    <script> var s=["2008-8-1","2009/9/2","10/3/2010"]; for(var i=0; ...

  3. PHP学习课程和培训方向学习路线分享

    php语言的优越性,集结了很多的开发爱好者,无论行业前景和个人发展来说,php正飞速的发展,php在不断兼容着类似closures和命名空间 等技术,同时兼顾性能和当下流行的框架.版本是7之后,一直在 ...

  4. [原]解决phpstudy下的nginx无法运行的问题

    一直在用phpstudy下的apache,今天忽然想切换到nginx,出现了一些错误,最终还是解决了. 之前是php 5.3 + apache 现在是php 5.3n + nginx 问题就出在这n上 ...

  5. SpringMVC+Mybatis框架搭建

    一.新建javaweb项目,并建好相应的包结构 二.添加项目jar到lib目录下 三.在config包中新建配置文件 sping-mvc.xml,内容如下: <?xml version=&quo ...

  6. C语言实现简易扫雷

    首先,写代码之前要将整体思路写出来: 扫雷游戏:1.需要两个二维数组,一个用来展示,一个用来放雷; 2.整体骨架在代码中都有注释说明; 3.游戏难度比较简单,适合初学者观看,如果有大佬看明白,可以指点 ...

  7. Mult

    public class MULT { public static void main(String args[]) { int k; for(int x=1;x<=9;x++) { for(i ...

  8. 基于vue来开发一个仿饿了么的外卖商城(二)

    一.抽出头部作为一个组件,在底部导航的时候可以相应的显示不同的标题 技术点:使用slot进行组件间的通信:父组件给子组件传值(子组件里面通过props接收父组件传过来的数据) 查看链接:https:/ ...

  9. Android面试收集录 OpenGL ES

    1.如何用OpenGL ES绘制一个三角形? 编写一个类实现Renderer接口,实现onDrawFrame方法,onSurfaceChanged方法,onSurfaceCreated方法 编写一个类 ...

  10. 2007: [Noi2010]海拔

    2007: [Noi2010]海拔 https://www.lydsy.com/JudgeOnline/problem.php?id=2007 分析: 平面图最小割. S在左下,T在右上,从S到T的一 ...