- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.java
More file actions
Latest commit
34 lines (30 loc) · 937 Bytes
/
Copy path3Sum.java
File metadata and controls
34 lines (30 loc) · 937 Bytes
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
importjava.util.*;
classSolution {
publicList<List<Integer>> threeSum(int[] nums) {
ArrayList<List<Integer>> list = newArrayList();
Arrays.sort(nums);
for(inti=0;i<nums.length;i++){
if((i>0)&&(nums[i]==nums[i-1])) continue;
intj=i+1;
intk = nums.length-1;
while(j<k){
if(nums[i]+nums[j]+nums[k] < 0){
j++;
}
elseif(nums[i]+nums[j]+nums[k] > 0){
k--;
}
else{
ArrayList<Integer> l1 = newArrayList();
l1.add(nums[i]);
l1.add(nums[j]);
l1.add(nums[k]);
if(!list.contains(l1)) list.add(l1);
j++;
k--;
}
}
}
returnlist;
}
}