LeetCode_18 4Sum
2024-09-30 20:21:20
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;
}
最新文章
- 给hadoop新手的一封信:Hadoop入门自学及对就业的帮助
- SQL Server 导出数据到 PostgreSQL
- Java语法糖4:内部类
- IIS Express 虚拟目录
- 转:python webdriver API 之操作测试对象
- System.out.println(对象)
- 项目知识点.Part3
- uva 10560 - Minimum Weight(数论)
- STL中vector,Map,Set的实现原理
- Round Numbers(poj 3252)
- Open-Domain QA -paper
- ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39; (2)
- python之路--管道, 事件, 信号量, 进程池
- javascript实现限定高度下文字随不同设备自适应改变字体大小至字数完全展示
- Hibernate一对多OnetoMany
- C# 性能优化 之 秒表 Stopwatch。
- django之Q
- msf客户端渗透(七):跳板、post模块、自动运行脚本
- 小程序码B接口生成出错:场景内容包含非法字符
- 优云软件应邀出席 ITSS 数据中心运营管理工作组 2017 年春季研讨会
热门文章
- Android 4.4.2 动态加入JNI库方法记录 (一 JNI库层)
- cojs 1001. [WZOI2011 S3] 消息传递
- Android开发之接收系统广播消息
- xcodebuild&#39; requires Xcode, but active developer directory &#39;/Library/Developer/CommandLineTools&#39; is
- [Codeforces 449B] Jzzhu and Cities
- 22 WPF列表,树,网格
- Java IO流中 File文件对象与Properties类(四)
- linux下的C语言开发 进程创建 延伸的几个例子
- bzoj 1652: [Usaco2006 Feb]Treats for the Cows【区间dp】
- bzoj 2442: [Usaco2011 Open]修剪草坪【单调栈】