Skip to content

Commit 5f52afe

Browse files
refactor 15
1 parent 496a031 commit 5f52afe

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
/**
88
* 15. 3Sum
9+
*
910
* Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
1011
* Find all unique triplets in the array which gives the sum of zero.
11-
12-
Note: The solution set must not contain duplicate triplets.
12+
*
13+
* Note: The solution set must not contain duplicate triplets.
1314
1415
For example, given array S = [-1, 0, 1, 2, -1, -4],
1516
@@ -22,39 +23,38 @@
2223

2324
public class _15 {
2425

25-
public List<List<Integer>> threeSum(int[] nums) {
26-
List<List<Integer>> result = new ArrayList<>();
27-
if (nums == null || nums.length == 0) {
28-
return result;
29-
}
30-
31-
Arrays.sort(nums);
32-
for (int i = 0; i < nums.length; i++) {
33-
if (i >= 1 && nums[i] == nums[i - 1]) {
34-
continue;
35-
}
36-
int left = i + 1;
37-
int right = nums.length - 1;
38-
while (left < right) {
39-
int sum = nums[i] + nums[left] + nums[right];
40-
if (sum == 0) {
41-
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
42-
/**be sure to skip duplicates*/
43-
while (left + 1 < right && nums[left] == nums[left + 1]) {
26+
public static class Solution1 {
27+
public List<List<Integer>> threeSum(int[] nums) {
28+
Arrays.sort(nums);
29+
List<List<Integer>> result = new ArrayList<>();
30+
for (int i = 0; i < nums.length - 2; i++) {
31+
if (i >= 1 && nums[i] == nums[i - 1]) {
32+
continue;
33+
}
34+
int left = i + 1;
35+
int right = nums.length - 1;
36+
while (left < right) {
37+
int sum = nums[i] + nums[left] + nums[right];
38+
if (sum == 0) {
39+
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
40+
41+
while (left + 1 < right && nums[left] == nums[left + 1]) {
42+
left++;
43+
}
44+
45+
while (right - 1 > left && nums[right] == nums[right - 1]) {
46+
right--;
47+
}
4448
left++;
45-
}
46-
while (right - 1 > left && nums[right] == nums[right - 1]) {
4749
right--;
50+
} else if (sum > 0) {
51+
right--;
52+
} else {
53+
left++;
4854
}
49-
left++;
50-
right--;
51-
} else if (sum > 0) {
52-
right--;
53-
} else {
54-
left++;
5555
}
5656
}
57+
return result;
5758
}
58-
return result;
5959
}
6060
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._15;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _15Test {
14+
private static _15.Solution1 solution1;
15+
private static int[] nums;
16+
private static List<List<Integer>> expected;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _15.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
nums = new int[]{-1, 0, 1, 2, -1, -4};
26+
expected = new ArrayList<>();
27+
expected.add(Arrays.asList(-1, -1, 2));
28+
expected.add(Arrays.asList(-1, 0, 1));
29+
assertEquals(expected, solution1.threeSum(nums));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)