-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.java
More file actions
70 lines (63 loc) · 2.35 KB
/
Copy path3Sum.java
File metadata and controls
70 lines (63 loc) · 2.35 KB
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
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) return Collections.emptyList();
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
int i = 0;
int j = 0;
int k = 0;
int n = nums.length;
while (i < n) {
j = i + 1;
k = n - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum == 0) {
result.add(Arrays.asList(nums[i], nums[j], nums[k]));
// advance j to the next non-dup j
// j < n-1 => j < k is more elegant, ask why?
while (j < k && nums[j+1] == nums[j]) j++;
j++;
// advance k to the next non-dup k
// k > 0 => j < k, the same as above
while (j < k && nums[k-1] == nums[k]) k--;
k--;
} else if (sum < 0) j++;
else k--;
}
while (i < n-1 && nums[i+1] == nums[i]) i++;
i++;
}
return result;
}
}
// --------------------------------
// i-> j-> <-k
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if(nums == null || nums.length <= 2) return Collections.emptyList();
Arrays.sort(nums);
int n = nums.length;
List<List<Integer>> result = new ArrayList<>();
for(int i = 0; i < n; i++) {
if(i > 0 && nums[i] == nums[i-1]) continue;
int j = i+1;
int k = n-1;
while(j < k) {
int sum = nums[i] + nums[j] + nums[k];
if(sum == 0) {
result.add(Arrays.asList(nums[i], nums[j], nums[k]));
j++;
while(j < n && nums[j-1] == nums[j]) j++;
while(k-1>= 0 && nums[k] == nums[k-1]) k--;
k--;
}else if (sum < 0) j++;
else k--;
}
}
return result;
}
}
// Solution#2: think it as a+b = -c. It will become 2sum problem
// Since there are n possibilites for c, we will have n * time complexity of 2Sum
// O(n^2) will be the complexity if we take this approach, which is the same as Solution#1