-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTripleSum.java
42 lines (38 loc) · 1.35 KB
/
TripleSum.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TripleSum {
public static void main(String[] args) {
TripleSum tripleSum = new TripleSum();
var triplets = tripleSum.threeSum(new int[]{-2,0,0,2,2});
System.out.println(triplets);
}
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> returnList = new ArrayList<>();
Arrays.sort(nums);
int pointer =0;
for(int i=0; i<nums.length-1; i++){
if(i>0 && nums[i] == nums[i-1]) //avoid duplicates
continue;
for(int low=i+1, high=nums.length-1; low<high;){
if (nums[i]+nums[low]+nums[high] == 0){
var newList = List.of(nums[i],nums[low],nums[high]);
if(pointer > 0 && returnList.get(pointer-1).equals(newList)){
low ++;
high --;
}
else{
returnList.add(newList);
pointer++;
low ++;
high--;
}
} else if(nums[i]+nums[low]+nums[high] > 0)
high--;
else
low++;
}
}
return returnList;
}
}