Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
74 lines (71 loc) · 2.27 KB

File metadata and controls

74 lines (71 loc) · 2.27 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package leetcode.all.solution1_100;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 18. 四数之和
*
* 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
*
* 注意:
*
* 答案中不可以包含重复的四元组。
*
* 示例:
*
* 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
*
* 满足要求的四元组集合为:
* [
* [-1, 0, 0, 1],
* [-2, -1, 1, 2],
* [-2, 0, 0, 2]
* ]
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class Solution18 {
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 ; i++) {
if(i>0&&nums[i]==nums[i-1]) continue;//对第1个数去重
for (int j = i + 1; j < nums.length ; j++) {
if(j>i+1&&nums[j]==nums[j-1]) continue;//对第2个数去重
int k = j + 1;//前指针
int v = nums.length - 1;//后指针
while (k < v) {
int sum = nums[i] + nums[j] + nums[k] + nums[v];
if (sum == target) {
res.add(Arrays.asList(nums[i], nums[j], nums[k], nums[v]));
//去重
while (k < v && nums[k] == nums[k + 1]) k++;
while (k < v && nums[v] == nums[v - 1]) v--;
k++;
v--;
} else if (sum > target) {
v--;
} else {
k++;
}
}
}
}
return res;
}
public static void main(String[] args) {
int[] nums = {
-1,2,2,-5,0,-1,4
};
int target = 3;
List<List<Integer>> r = new Solution18().fourSum(nums, target);
for (List<Integer> l : r) {
for (Integer i : l) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.