-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddThreeNumbers.java
73 lines (64 loc) · 1.73 KB
/
AddThreeNumbers.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package leetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @className AddThreeNumber.java
* @author SakrA
* @version 创建时间:2019年3月19日 上午9:47:21
*/
public class AddThreeNumbers {
public static List<List<Integer>> threeSum(int[] nums) {
int len = nums.length;
int i = 0, left = 0, right = 0;
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
for (i = 0; i < len - 2; i++) {
if (nums[i] > 0)
break;
if (i >= 1 && nums[i] == nums[i - 1] && i < len - 2)
continue;
left = i + 1;
right = len - 1;
while (left < right) {
if (nums[left] + nums[right] == -nums[i]) {
list.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--;
}
} else if (nums[left] + nums[right] < -nums[i]) {
left++;
while (left < right && nums[left] == nums[left - 1]) {
left++;
}
} else {
right--;
while (left < right && nums[right] == nums[right + 1]) {
right--;
}
}
}
}
return list;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums1 = { -4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6 };
List<List<Integer>> list = threeSum(nums1);
System.out.println(Arrays.toString(nums1));
for (List<Integer> item : list) {
System.out.println(Arrays.toString(item.toArray()));
}
int[] nums2 = { -2, 0, 1, 1, 2 };
list = threeSum(nums2);
System.out.println(Arrays.toString(nums2));
for (List<Integer> item : list) {
System.out.println(Arrays.toString(item.toArray()));
}
}
}