-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum4.java
More file actions
33 lines (32 loc) · 1.09 KB
/
Copy pathSum4.java
File metadata and controls
33 lines (32 loc) · 1.09 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Sum4 {
public static List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (nums.length < 4) {
return result;
}
Arrays.sort(nums);
int leftPos = 0;
while (leftPos < nums.length -3) {
int rightPos = nums.length - 1;
while (rightPos > 2) {
int leftIndex = leftPos + 1;
int rightIndex = rightPos - 1;
while (leftIndex < rightIndex) {
int sum = nums[leftPos] + nums[leftIndex] + nums[rightIndex] + nums[rightPos];
if (sum == target)
result.add(Arrays.asList(nums[leftPos], nums[leftIndex], nums[rightIndex], nums[rightPos]));
if (sum <= target)
while(nums[leftIndex] == nums[++leftIndex] && leftIndex < rightIndex);
if (sum >= target)
while(nums[rightIndex] == nums[--rightIndex] && leftIndex < rightIndex);
}
while(nums[rightPos] == nums[--rightPos] && rightPos > 2);
}
while(nums[leftPos] == nums[++leftPos] && leftPos < nums.length -3);
}
return result;
}
}