-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0015-3sum.java
77 lines (60 loc) · 2.56 KB
/
0015-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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int low = 0;
int high = nums.length - 1;
int sum = 0;
for(int i = 0; i < nums.length - 1; i++){
low = i + 1;
high = nums.length - 1;
sum = 0 - nums[i];
if(i == 0 || (i > 0 && nums[i] != nums[i -1])){
while (low < high){
if(sum == nums[low] + nums[high] ){
List<Integer> temp = new ArrayList<>();
temp.add(nums[i]);
temp.add(nums[low]);
temp.add(nums[high]);
//Since its sorted and we are already handling duplicated by code below
// no need to check for duplicates in the res DS. it is complicating the algorithm
// if(!res.contains(temp)){
// res.add(temp);
// }
res.add(temp);
while( low < high && nums[low] == nums[low + 1]) low++;
while( low < high && nums[high] == nums[high - 1]) high--;
low++;
high--;
}
else if (sum > nums[low] + nums[high] ) low++;
else high--;
}
}
}
return res;
}
}
// Approach 1
// List<List<Integer>> res = new ArrayList<>();
// for (int i = 0; i < nums.length -2 ; i++){
// for(int j = i + 1; j < nums.length -1 ; j++){
// List<Integer> temp = new ArrayList<>();
// for(int k = j + 1; k < nums.length; k++){
// if (nums[i] + nums[j] + nums[k] == 0){
// temp.add(nums[i]);
// temp.add(nums[j]);
// temp.add(nums[k]);
// Collections.sort(temp);
// }
// System.out.println(temp);
// if (temp.size() != 0 && !res.contains(temp)){
// System.out.println( "222" + temp);
// res.add(temp);
// // temp.clear();
// }
// temp.clear();
// }
// }
// }
// return res;