-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00015-3sum.java
59 lines (46 loc) · 1.38 KB
/
00015-3sum.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 15: 3Sum
// https://leetcode.com/problems/3sum/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
// SOLUTION
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for (int i=0; i<nums.length; i++) {
if (i>0 && nums[i]==nums[i-1]) continue;
int l = i+1;
int r = nums.length - 1;
while (l<r) {
int s = nums[i] + nums[l] + nums[r];
if (s>0) r--;
else if (s<0) l++;
else {
result.add(Arrays.asList(nums[i], nums[l], nums[r]));
while (nums[l]==nums[l+1]) l++;
while (nums[r]==nums[r-1]) r--;
l++;
r--;
}
}
}
return result;
}
public static void main(String[] args) {
Solution o = new Solution();
// INPUT
int[] nums = {-1,0,1,2,-1,-4};
// OUTPUT
var result = o.threeSum(nums);
System.out.print("[");
for (var x : result) {
System.out.print("[");
for (var y : x) {
System.out.print(y+" ");
}
System.out.print("\b] ");
}
System.out.println("\b]");
}
}