LeetCode_18 4Sum
Given an array nums
of n integers and an integer target
, are there elements a, b, c, and d in nums
such that a + b+ c + d = target
? Find all unique quadruplets in the array which gives the sum of target
.
Note:
The solution set must not contain duplicate quadruplets.
Example:
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
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 k = j + 1;
int l = nums.length - 1;
while (k < l) {
int sum = nums[i] + nums[j] + nums[k] + nums[l];
if (sum == target) {
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[k]);
list.add(nums[l]);
res.add(list);
k++;
l--;
// 去重复
while (k < l && nums[k] == nums[k - 1]) {
k++;
}
while (k < l && nums[l] == nums[l + 1]) {
l--;
}
} else if (sum < target) {
k++;
} else {
l--;
}
}
}
}
return res;
}
public List<List<Integer>> fourSum3(int[] num, int target) {
ArrayList<List<Integer>> ans = new ArrayList<>();
if (num.length < 4)
return ans;
Arrays.sort(num);
for (int i = 0; i < num.length - 3; i++) {
if (num[i] + num[i + 1] + num[i + 2] + num[i + 3] > target)
break; // first candidate too large, search finished
if (num[i] + num[num.length - 1] + num[num.length - 2] + num[num.length - 3] < target)
continue; // first candidate too small
if (i > 0 && num[i] == num[i - 1])
continue; // prevents duplicate result in ans list
for (int j = i + 1; j < num.length - 2; j++) {
if (num[i] + num[j] + num[j + 1] + num[j + 2] > target)
break; // second candidate too large
if (num[i] + num[j] + num[num.length - 1] + num[num.length - 2] < target)
continue; // second candidate too small
if (j > i + 1 && num[j] == num[j - 1])
continue; // prevents duplicate results in ans list
int low = j + 1, high = num.length - 1;
while (low < high) {
int sum = num[i] + num[j] + num[low] + num[high];
if (sum == target) {
ans.add(Arrays.asList(num[i], num[j], num[low], num[high]));
while (low < high && num[low] == num[low + 1])
low++; // skipping over duplicate on low
while (low < high && num[high] == num[high - 1])
high--; // skipping over duplicate on high
low++;
high--;
}
// move window
else if (sum < target)
low++;
else
high--;
}
}
}
return ans;
}
LeetCode_18 4Sum的更多相关文章
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [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:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum
18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c ...
- No.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 = ...
- 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 3Sum & 4Sum
3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...
- 【leetcode】4Sum
4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum
2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...
随机推荐
- [办公自动化]excel工作簿内的表无法删除,单击右键无删除键
今天同事问,我自己的工作簿,没有设置保护,但是就是无法删除其中的工作表. 后来发现,她的excel工作簿打开的文件名后面显示[共享]. 原因找到了. 取消共享就可以了.
- vim升级到8.0
1.卸载 sudo apt-get remove --purge vim 2.添加8.0的vim源并安装 sudo add-apt-repository ppa:jonathonf/vim sudo ...
- UVA 315 求连通图里的割点
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20837 哎 大白书里求割点的模板不好用啊,许多细节理解起来也好烦..还好找了 ...
- [ZJOI 2007] 矩阵游戏
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1059 [算法] 二分图最大匹配 时间复杂度 : O(N^3) [代码] #inclu ...
- python-----删除文件到回收站
python删除文件一般使用os.remove,但这样删是直接删除文件,不删到回收站的,那么想删除文件到回收站怎么办? 这时,就需要使用shell模块了 from win32com.shell imp ...
- SVN导出指定版本差异文件 ***
当一个项目进入运营维护阶段以后,不会再频繁地更新全部源文件到服务器,这个时间的修改大多是局部的,因此更新文件只需更新修改过的文件,其他 没有修改过的文件就没有必要上载到服务器.但一个稍微上规模的项目文 ...
- openstack 配置dnsmasq 域名解析
- Hadoop 分布式环境slave节点重启忽然不好使了
Hadoop 分布式环境slaves节点重启: 忽然无法启动DataNode和NodeManager处理: 在master节点: vim /etc/hosts: 修改slave 节点的IP (这个时候 ...
- struct框架
配置文件struct-config.xml<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE str ...
- 一句powershell调用mimikatz抓密码
mimikatz神器大家都知道吧,可以抓取系统内的明文密码,但是平时我们测试的时候需要把mimikatz的几个文件上传到目标系统上面,然后再手工执行几个命令才能搞定,今天无意访问一个大神的博客,发现其 ...