-
Notifications
You must be signed in to change notification settings - Fork 2
/
ThreeSum.java
77 lines (68 loc) · 2.24 KB
/
ThreeSum.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package arrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 15. 3Sum
*
* Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0?
* Find all unique triplets in the array which gives the sum of zero.
*
* Notice that the solution set must not contain duplicate triplets.
*
*
*
* Example 1:
*
* Input: nums = [-1,0,1,2,-1,-4]
* Output: [[-1,-1,2],[-1,0,1]]
*
* I have this solution coded under frequent/medium as well.
*/
public class ThreeSum {
public static void main(String[] args) {
int[] nums = {1, -1, -1, 0};
ThreeSum tSum = new ThreeSum();
List<List<Integer>> sums = tSum.threeSum(nums);
for (List<Integer> l : sums) {
System.out.println(l);
}
}
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> listSums = new ArrayList<>();
for (int i = 0; i < nums.length && nums[i] <= 0; i++) {
if (i == 0 || nums[i - 1] != nums[i]) { //avoid duplicate checks
findSums(nums[i], i + 1, nums, listSums);
}
}
return listSums;
}
/**
* Use a two pointer approach, start from smallest on left and largest on right then advance the right or left
* pointer based on whether the sum from left and right indexes is less or greater than the target
*
*/
void findSums(int start, int startIndex, int[] nums,
List<List<Integer>> lSums) {
int target = start * -1;
int i = startIndex;
int j = nums.length - 1;
while (j > i) {
int currSum = nums[i] + nums[j];
if (currSum > target) {
j--; //make currSum smaller
} else if (currSum < target) {
i++; //make currSum larger
} else {
lSums.add(Arrays.asList(start, nums[i], nums[j]));
i++;
j--;
//advance pointers if repeated items
//so that the next data set has a unique collection
while (i < nums.length && nums[i] == nums[i - 1]) i++;
while (nums[j] == nums[j + 1] && j > 0) j--;
}
}
}
}