-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
59 lines (55 loc) · 1.91 KB
/
Solution.java
File metadata and controls
59 lines (55 loc) · 1.91 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
package leetCode_18;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @author dimdark
* @date 2017-10-04
* @time 8:04 PM
*/
public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> rst = new LinkedList<List<Integer>>();
if (nums == null || nums.length < 4) return rst;
Arrays.sort(nums);
int i = 0, j;
while (i < nums.length - 3) {
j = i + 1;
while (j < nums.length - 2) {
int newTarget = target - nums[i] - nums[j];
int leftIdx = j + 1, rightIdx = nums.length - 1;
while (leftIdx < rightIdx) {
int sum = nums[leftIdx] + nums[rightIdx];
if (newTarget < sum) {
--rightIdx;
} else if (newTarget > sum) {
++leftIdx;
} else {
List<Integer> list = new LinkedList<Integer>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[leftIdx]);
list.add(nums[rightIdx]);
rst.add(list);
++leftIdx; --rightIdx;
while (leftIdx < nums.length && nums[leftIdx - 1] == nums[leftIdx]) {
++leftIdx;
}
while (rightIdx > j && nums[rightIdx + 1] == nums[rightIdx]) {
--rightIdx;
}
}
}
++j;
while (j < nums.length - 2 && nums[j - 1] == nums[j]) {
++j;
}
}
++i;
while (i < nums.length - 3 && nums[i - 1] == nums[i]) {
++i;
}
}
return rst;
}
}