-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·52 lines (47 loc) · 1.6 KB
/
Solution.java
File metadata and controls
executable file
·52 lines (47 loc) · 1.6 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
41
42
43
44
45
46
47
48
49
50
51
52
package $015;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 9:54 2018/4/2.
*/
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> arrayLists = new ArrayList<>();
if (nums == null || nums.length < 3){
return arrayLists;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++){
if (i > 0 && i < nums.length - 1 && nums[i] == nums[i-1]){ // important
continue;
}
int left = i + 1, right = nums.length - 1, sum = -nums[i];
while (left < right){
if (nums[left] + nums[right] == sum){
arrayLists.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (left < right && nums[left] == nums[left+1]){
left++;
}
left++;
while (right > left && nums[right] == nums[right-1]){
right--;
}
right--;
}else if (nums[left] + nums[right] < sum){
while (left < right && nums[left] == nums[left+1]){
left++;
}
left++;
}else {
while (right > left && nums[right] == nums[right-1]){
right--;
}
right--;
}
}
}
return arrayLists;
}
}