-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_18.java
More file actions
72 lines (69 loc) · 2.46 KB
/
Solution_18.java
File metadata and controls
72 lines (69 loc) · 2.46 KB
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
/*
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]
]
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
public static void main ( String[] args ) {
int[] nums = new int[]{1,-2,-5,-4,-3,3,3,5}; //-5, -4,-3,-2,1,3,3,5
int target = -11;
List<List<Integer>> ans= fourSum(nums, target);
for (int i = 0 ; i < ans.size(); i++ ){
System.out.println(ans.get(i).toString());
}
}
public static List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
int n = nums.length;
List<List<Integer>> ans = new ArrayList<>();
for ( int i = 0 ; i < n - 3 ; i++ ) {
if ( i - 1 >= 0 && nums[i] == nums[i-1] )
continue;
for ( int j = i + 1 ; j < n - 2 ; j++ ) {
if ( j - 1 >= i + 1 && nums[j] == nums[j-1] )
continue;
int L = j + 1 ;
int R = n - 1;
while ( L < R ) {
int sum = nums[i] + nums[j] + nums[L] + nums[R];
if ( sum == target) {
List<Integer> tmp = new ArrayList<>();
tmp.add(nums[i]);
tmp.add(nums[j]);
tmp.add(nums[L]);
tmp.add(nums[R]);
ans.add(tmp);
while (L < R && nums[L] == nums[L + 1])
L++;
while (L < R && nums[R] == nums[R - 1])
R--;
L++;
R--;
} else if ( sum > target ) {
while (L < R && nums[R] == nums[R - 1])
R--;
R--;
} else if ( sum < target ) {
while (L < R && nums[L] == nums[L + 1])
L++;
L++;
}
}
}
}
return ans;
}
}