Skip to content

Commit 124a464

Browse files
committed
add solution for 3sum (two pointers)
1 parent a95d0b7 commit 124a464

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

3sum/Ujoonnee.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
import java.util.*;
2+
3+
class Solution {
4+
public List<List<Integer>> threeSum(int[] nums) {
5+
Arrays.sort(nums);
6+
Set<List<Integer>> set = new HashSet<>();
7+
for(int i = 0; i < nums.length && nums[i] <= 0; i++) {
8+
int j = i+1;
9+
int k = nums.length - 1;
10+
while(j < k) {
11+
int sum = nums[i] + nums[j] + nums[k];
12+
if (sum > 0) {
13+
k--;
14+
continue;
15+
}
16+
17+
if (sum < 0) {
18+
j++;
19+
continue;
20+
}
21+
22+
set.add(List.of(nums[i], nums[j], nums[k]));
23+
j++;
24+
k--;
25+
}
26+
}
27+
28+
return new ArrayList<>(set);
29+
}
30+
}
31+
32+
/*
33+
import java.util.*;
234
import java.util.stream.Collectors;
335
436
class Solution {
@@ -51,4 +83,5 @@ public boolean equals(Object o) {
5183
public int hashCode() {
5284
return Objects.hash(triplet[0], triplet[1], triplet[2]);
5385
}
54-
}
86+
}
87+
*/

0 commit comments

Comments
 (0)