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. c#分析SQL语句

    最近总结了c#一般的功能,然后自己在博文中写了很多东西.主要是在用途上面.能够解决一些问题.现在分各个组件和方向写完了.主要的内容写了demo,也写了自己的项目组件和模型. 最后一个SQL分析.其实在 ...

  2. WebGL学习笔记(3)

    根据上篇笔记,在对3D对象可进行普通的控制后,以及学习了http://hiwebgl.com的教程第10章内容:世界模型的载入以及控制镜头移动,经过多次调试矩阵代码,已经可以实现在世界中旋转镜头/控制 ...

  3. Javascript中的this对象

    对于this的使用,我们最常遇到的主要有,在全局函数中,在对象方法中,call和apply时,闭包中,箭头函数中以及class中: 我们知道this对象是在运行时基于函数的执行环境绑定的,在调用函数之 ...

  4. Mybatis中使用UpdateProvider注解实现根据主键批量更新

    Mapper中这样写: @UpdateProvider(type = SjjcSqlProvider.class, method = "updateTaskStatusByCBh" ...

  5. 查找mysql中未提交的事务

    1.查找未提交事务 在mysql中运行: select t.trx_mysql_thread_id from information_schema.innodb_trx t 2.删除线程 kill   ...

  6. python核心编程2 第七章 练习

    7-4. 建立字典. 给定两个长度相同的列表,比如说,列表[1, 2, 3,...]和['abc', 'def','ghi',...],用这两个列表里的所有数据组成一个字典,像这样:{1:'abc', ...

  7. winform Treeview控件使用

    做角色菜单权限时用到treeview控件做树状显示菜单,简单总结了一下用法: 1.在winform窗体中拖入treeview控件,注意修改属性CheckBoxes属性为true,即在节点旁显示复选框 ...

  8. jquery点击li 获取当前父节点所在类的索引

    jquery点击li 获取当前父节点所在类的索引 $('.jbcz').find('.content li').click(function(){ //alert($('.jbcz').find('. ...

  9. Apache Maven(五):插件

    Maven的插件分如下两种: build plugins:该插件在项目构建阶段执行,它们都在<build>标签中设置. reporting plugins : 该插件在网站生成期间执行,他 ...

  10. Mina 组件介绍之 IoBuffer

    在Java NIO 中,ByteBuffer通常作为通信中传递消息的载体.而在Mina中,采用了IoBuffer代替ByteBuffer.Mina给出了不用ByteBuffer的两个主要理由: 1.  ...