-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.3sum.java
37 lines (33 loc) · 1.22 KB
/
15.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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> threeSumList = new ArrayList<List<Integer>>();
if (nums.length<3) return threeSumList;
Arrays.sort(nums);
int start;
for (int i= 0; i<nums.length-2;i ++){
start = i+1;
int end = nums.length-1;
while (start<end){
int sum = nums[start]+nums [end];
if (sum + nums[i]==0){
List<Integer>numsList =new ArrayList<Integer>();
numsList.add(nums[start]);
numsList.add(nums[end]);
numsList.add(nums[i]);
if(!threeSumList.contains(numsList)){
threeSumList.add(numsList);
}
start ++;
end --;
}
else if(sum+nums[i]<0){
start ++;
}
else {
end --;
}
}
}
return threeSumList;
}
}