Skip to content

Commit 00009d9

Browse files
refactor 18
1 parent 3f89915 commit 00009d9

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

src/main/java/com/fishercoder/solutions/_18.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,44 @@
2323

2424
public class _18 {
2525

26-
public List<List<Integer>> fourSum(int[] nums, int target) {
27-
List<List<Integer>> result = new ArrayList();
28-
if (nums == null || nums.length == 0) {
29-
return result;
30-
}
31-
Arrays.sort(nums);
32-
for (int i = 0; i < nums.length - 3; i++) {
33-
if (i > 0 && nums[i - 1] == nums[i]) {
34-
continue;
26+
public static class Solution1 {
27+
public List<List<Integer>> fourSum(int[] nums, int target) {
28+
List<List<Integer>> result = new ArrayList();
29+
if (nums == null || nums.length == 0) {
30+
return result;
3531
}
36-
for (int j = i + 1; j < nums.length - 2; j++) {
37-
if (j > i + 1 && nums[j - 1] == nums[j]) {
32+
Arrays.sort(nums);
33+
for (int i = 0; i < nums.length - 3; i++) {
34+
if (i > 0 && nums[i - 1] == nums[i]) {
3835
continue;
3936
}
40-
int left = j + 1;
41-
int right = nums.length - 1;
42-
while (left < right) {
43-
int sum = nums[i] + nums[j] + nums[left] + nums[right];
44-
if (sum == target) {
45-
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
46-
while (left + 1 < right && nums[left] == nums[left + 1]) {
37+
for (int j = i + 1; j < nums.length - 2; j++) {
38+
if (j > i + 1 && nums[j - 1] == nums[j]) {
39+
continue;
40+
}
41+
int left = j + 1;
42+
int right = nums.length - 1;
43+
while (left < right) {
44+
int sum = nums[i] + nums[j] + nums[left] + nums[right];
45+
if (sum == target) {
46+
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
47+
while (left + 1 < right && nums[left] == nums[left + 1]) {
48+
left++;
49+
}
50+
while (right - 1 > left && nums[right] == nums[right - 1]) {
51+
right--;
52+
}
4753
left++;
48-
}
49-
while (right - 1 > left && nums[right] == nums[right - 1]) {
5054
right--;
55+
} else if (sum > target) {
56+
right--;
57+
} else {
58+
left++;
5159
}
52-
left++;
53-
right--;
54-
} else if (sum > target) {
55-
right--;
56-
} else {
57-
left++;
5860
}
5961
}
6062
}
63+
return result;
6164
}
62-
return result;
6365
}
64-
6566
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._18;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _18Test {
9+
private static _18.Solution1 solution1;
10+
private static int[] nums;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _18.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
nums = new int[]{1, 0, -1, 0, -2, 2};
20+
CommonUtils.printListList(solution1.fourSum(nums, 0));
21+
}
22+
23+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy