-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15. 3Sum
33 lines (33 loc) · 1002 Bytes
/
15. 3Sum
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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int i = 0 ; i < nums.length - 2 ; i++){
int s = i+1 , e = nums.length - 1;
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
while(s < e){
int sum = nums[i] + nums[s] + nums[e];
if( sum == 0){
res.add(Arrays.asList(nums[i] , nums[s], nums[e]));
s++;
e--;
while(s < e && nums[s] == nums[s-1]){
s++;
}
while(s < e && nums[e] == nums[e+1]){
e--;
}
}
else if(sum > 0){
e--;
}
else{
s++;
}
}
}
return res;
}
}