|
1 | 1 | package g0001_0100.s0018_4sum; |
2 | 2 |
|
3 | | -// #Medium #Array #Sorting #Two_Pointers #2022_10_07_Time_6_ms_(96.23%)_Space_44.3_MB_(76.54%) |
| 3 | +// #Medium #Array #Sorting #Two_Pointers #2023_03_15_Time_3_ms_(99.47%)_Space_43.2_MB_(35.71%) |
4 | 4 |
|
5 | 5 | import java.util.ArrayList; |
6 | 6 | import java.util.Arrays; |
|
9 | 9 | @SuppressWarnings("java:S135") |
10 | 10 | public class Solution { |
11 | 11 | public List<List<Integer>> fourSum(int[] nums, int target) { |
12 | | - List<List<Integer>> ret = new ArrayList<>(); |
13 | | - if (nums.length < 4) { |
14 | | - return ret; |
15 | | - } |
16 | | - if (nums[0] == 1000000000 && nums[1] == 1000000000) { |
17 | | - return ret; |
18 | | - } |
| 12 | + int n = nums.length; |
19 | 13 | Arrays.sort(nums); |
20 | | - for (int i = 0; i < nums.length - 3; i++) { |
21 | | - if (i != 0 && nums[i] == nums[i - 1]) { |
| 14 | + List<List<Integer>> result = new ArrayList<>(); |
| 15 | + for (int i = 0; i < n - 3; i++) { |
| 16 | + if (i > 0 && nums[i] == nums[i - 1]) { |
| 17 | + continue; |
| 18 | + } |
| 19 | + if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) { |
| 20 | + break; |
| 21 | + } |
| 22 | + if ((long) nums[i] + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) { |
22 | 23 | continue; |
23 | 24 | } |
24 | | - for (int j = i + 1; j < nums.length - 2; j++) { |
25 | | - if (j != i + 1 && nums[j] == nums[j - 1]) { |
| 25 | + for (int j = i + 1; j < n - 2; j++) { |
| 26 | + if (j > i + 1 && nums[j] == nums[j - 1]) { |
26 | 27 | continue; |
27 | 28 | } |
28 | | - int left = j + 1; |
29 | | - int right = nums.length - 1; |
30 | | - int half = nums[i] + nums[j]; |
31 | | - if (half + nums[left] + nums[left + 1] > target) { |
32 | | - continue; |
| 29 | + if ((long) nums[j] + nums[j + 1] + nums[j + 2] > target - nums[i]) { |
| 30 | + break; |
33 | 31 | } |
34 | | - if (half + nums[right] + nums[right - 1] < target) { |
| 32 | + if ((long) nums[j] + nums[n - 2] + nums[n - 1] < target - nums[i]) { |
35 | 33 | continue; |
36 | 34 | } |
37 | | - while (left < right) { |
38 | | - int sum = nums[left] + nums[right] + half; |
39 | | - if (sum == target) { |
40 | | - ret.add(Arrays.asList(nums[left++], nums[right--], nums[i], nums[j])); |
41 | | - while (nums[left] == nums[left - 1] && left < right) { |
42 | | - left++; |
| 35 | + int tempTarget = target - (nums[i] + nums[j]); |
| 36 | + int low = j + 1; |
| 37 | + int high = n - 1; |
| 38 | + while (low < high) { |
| 39 | + int curSum = nums[low] + nums[high]; |
| 40 | + if (curSum == tempTarget) { |
| 41 | + List<Integer> tempList = new ArrayList<>(); |
| 42 | + tempList.add(nums[i]); |
| 43 | + tempList.add(nums[j]); |
| 44 | + tempList.add(nums[low]); |
| 45 | + tempList.add(nums[high]); |
| 46 | + result.add(tempList); |
| 47 | + low++; |
| 48 | + high--; |
| 49 | + while (low < high && nums[low] == nums[low - 1]) { |
| 50 | + low++; |
43 | 51 | } |
44 | | - while (nums[right] == nums[right + 1] && left < right) { |
45 | | - right--; |
46 | | - } |
47 | | - } else if (sum < target) { |
48 | | - left++; |
49 | | - while (nums[left] == nums[left - 1] && left < right) { |
50 | | - left++; |
| 52 | + while (low < high && nums[high] == nums[high + 1]) { |
| 53 | + high--; |
51 | 54 | } |
| 55 | + } else if (curSum < tempTarget) { |
| 56 | + low++; |
52 | 57 | } else { |
53 | | - right--; |
54 | | - while (nums[right] == nums[right + 1] && left < right) { |
55 | | - right--; |
56 | | - } |
| 58 | + high--; |
57 | 59 | } |
58 | 60 | } |
59 | 61 | } |
60 | 62 | } |
61 | | - return ret; |
| 63 | + return result; |
62 | 64 | } |
63 | 65 | } |
0 commit comments