-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThree_Sum.java
More file actions
40 lines (37 loc) · 1.19 KB
/
Copy pathThree_Sum.java
File metadata and controls
40 lines (37 loc) · 1.19 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
import java.util.*;
class Three_Sum {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
int n = nums.length;
for(int i =0 ; i<n-2; i++){
int left = i+1;
int right = n-1 ;
if (i>0 && nums[i]== nums[i-1]){
continue;
}
else{
while(left<right){
if(nums[left]+nums[right]> -nums[i]){
right--;
}
else if(nums[left]+ nums[right]< -nums[i]){
left++ ;
}
else{
result.add(Arrays.asList(nums[i],nums[left],nums[right]));
left++;
right -- ;
while(left<right && nums[left]== nums[left-1]){
left ++;
}
while(left<right && nums[right]==nums[right+1]){
right--;
}
}
}
}
}
return result;
}
}