018 4Sum 四个数的和
给定一个含有 n 个整数的数组 S,数列 S 中是否存在元素 a,b,c 和 d 使 a + b + c + d = target ?
请在数组中找出所有满足各元素相加等于特定值的不重复组合。
注意:解决方案集不能包含重复的四元组合。
例如,给定数组 S = [1, 0, -1, 0, -2, 2],并且给定 target = 0。
示例答案为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
详见:https://leetcode.com/problems/4sum/description/
实现语言:Java
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List result = new ArrayList<List<Integer>>();
if (nums == null || nums.length < 4){
return result;
} Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i-1]){
continue;
} for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j-1]){
continue;
} int left = j + 1;
int right = nums.length - 1;
while (left < right) {
int tmp = nums[i] + nums[j] + nums[left] + nums[right];
if (tmp == target) {
List l = new ArrayList<Integer>();
l.add(nums[i]);
l.add(nums[j]);
l.add(nums[left]);
l.add(nums[right]);
result.add(l);
while (left < right && nums[++left] == nums[left-1]);
while (left < right && nums[--right] == nums[right+1]);
} else if (tmp > target){
while (left < right && nums[--right] == nums[right+1]);
} else{
while (left < right && nums[++left] == nums[left-1]);
}
}
}
}
return result;
}
}
参考:https://www.jianshu.com/p/1ec86ec8feb6
018 4Sum 四个数的和的更多相关文章
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- lintcode:四个数之和
题目 四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 样例 例如,对于给定的整数数组S=. 满足要求的四元组集合为: (-1, 0 ...
- js jq 手机号实现(344) 附带删除功能 jq 实现银行卡没四个数加一个空格 附带删除功能
js 手机号实现(344) 下面有将正则验证去掉“-” 或“空格” 下一篇博客有单独的删除功能方法 <!DOCTYPE html> <head> <meta char ...
- 18 4Sum(寻找四个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...
- 【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 = ...
- [LeetCode] 4Sum 四数之和
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- LeetCode 4Sum 4个数之和
题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...
- [LeetCode] 18. 4Sum 四数之和
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- poj 1348 Computing (四个数的加减乘除四则运算)
http://poj.org/problem?id=1348 Computing Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
随机推荐
- POJ1365:质因数分解
Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3590 Accepted: 1623 Descri ...
- idea-spark-sbt 打包jar
1.打开idea下的terminal窗口 2.只打包部分项目 sbt insight-import/clean insight-import/assembly 这表示只打包主目录下的insight- ...
- web攻击之二:CSRF跨站域请求伪造
CSRF是什么? (Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,它在 2007 年曾被列为互联网 20 大安全隐患之一,也被称为“One Click ...
- winform 客户端 HTTP协议与服务端通信以及解决中文乱码
本来从来没有仔细研究过Http协议,今天因为公司业务需求,调试了半天,终于现在会Winform用Http协议与服务端通信了,其中常用的有POST和Get方式: 仔细看了人人网和新浪等大部分都是采用GE ...
- [poj3311]Hie with the Pie(Floyd+状态压缩DP)
题意:tsp问题,经过图中所有的点并回到原点的最短距离. 解题关键:floyd+状态压缩dp,注意floyd时k必须在最外层 转移方程:$dp[S][i] = \min (dp[S \wedge (1 ...
- windows、Linux 测试服务器、电脑的某些个端口是否打开
测试远程端口是否开放包括两种方法: 一. 命令行的形式 二.代码 先参考我的博客 windows.Linux 开放端口 一.命令行的形式 两个命令:telnet.nc(netcat) 两种网络层协议: ...
- 用 R 画中国分省市地图
用 R 画中国分省市地图 (2010-11-18 16:25:34) 转载▼ 标签: 中国地图 营销 杂谈 分类: 数据分析 用R 也可以做出漂亮的依参数变化的中国地图. 主要参考(http://co ...
- 关于 char 和 unsigned char 的区别
首先卖个关子: 为什么网络编程中的字符定义一般都为无符号的字符? char buf[16] = {0}; unsigned char ubuf[16] = { 0 }; 上面两个定义的区别是: ...
- Boost Python学习笔记(三)
你将学到什么 在C++中调用Python代码时的传参问题 基础类型 继续使用前面的项目,但是先修改下Python脚本(zoo.py),添加Add和Str函数,分别针对整数.浮点数和字符串参数的测试 d ...
- BKMyFAQ
邮箱配置如图 发送格式: { "bk_app_code": "bk_monitor", #该字段可以查看文件:/data/install/.app.token ...