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. Python基础—07-函数使用(01)

    #函数使用 零碎知识 灵活的if-else a = 3 if False else 5 # 等价于 if False: a = 3 else: a = 5 灵活的and/or # 当前面为真,才会进行 ...

  2. c#项目总结

    写了将近10年代码了,最后休息,回想了下,感觉什么都没有. 所以打算写一些总结性的文章,先写几个项目,用于c#各个方向的封装使用 最后汇总成一个完善的解决方案.所有项目都在一个解决方案FastAIFr ...

  3. docker搭建基于percona-xtradb-cluster方案的mysql集群

    一.部署环境 序号 hostname ip 备注 1 manager107 10.0.3.107 centos7;3.10.0-957.1.3.el7.x86_64 2 worker68 10.0.3 ...

  4. wampserver 服务器报500错误,侦察小结

    Internal Server Error The server encountered an internal error or misconfiguration and was unable to ...

  5. JS 红包随机

    微信随机红包,指定金额指定用户,随机发送红包 var moneys = new Array(); var moneyTotal = 0; function rand(obj){ if(obj.size ...

  6. Hive(1)-基本概念

    一. 什么是Hive Hive:由Facebook开源用于解决海量结构化日志的数据统计. Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能. 本 ...

  7. 事物总线模式实例——EventBus实例详解

    事件总线模式是一种广泛运用于安卓开发之中的一种软件架构模式,而事件总线模式在安卓开发中最广泛的应用莫过于AndroidStudio提供的EventBus,所以我就EventBus来谈谈对事件总线模式的 ...

  8. AB PLC 编程之状态机

    AB的程序设计和西门子有点PLC不大一样,在AB中没有RS指令,所以主要用move指令来作步进.今天我们就用Move指令写个AB的程序,和西门子比,有哪些不同. 控制任务 很简单的一个状态机.初始步为 ...

  9. python与mysql的连接过程

    1.cmd---pip3 install PyMySQL2.>>>import pymysql3.mysql>create database bookdb character ...

  10. mongodb的学习之旅一

    描述 作为一枚菜鸟级别的coder,刚接触nodejs没有多久.现在在学习微信公众号的开发,但是碰到了mongodb保存用户数据的时候,出现了DeprecationWarning: Mongoose: ...