-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathThreeSum.java
More file actions
96 lines (91 loc) · 3.28 KB
/
Copy pathThreeSum.java
File metadata and controls
96 lines (91 loc) · 3.28 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package LeetCode.array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
* Find all unique triplets in the array which gives the sum of zero.
* Note: The solution set must not contain duplicate triplets.
*
* For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
* @author Administrator
*
*/
public class ThreeSum {
/** 所耗时间太长
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int length = nums.length;
List<Integer> surplusLeft = new ArrayList<>();
List<Integer> surplusLeftMedium = new ArrayList<>();
List<Integer> surplusMediumRight = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
for(int i = 0; i < length; i++) {
if (surplusLeft.contains(nums[i])) {
continue;
}
int left = nums[i];
for(int j = i+1; j < length; j++) {
if (surplusLeft.contains(nums[j]) || surplusLeftMedium.contains(nums[j])) {
continue;
}
int medium = nums[j];
int merge = medium + left;
for (int k = j+1; k < length; k++) {
if (surplusLeftMedium.contains(nums[k]) || surplusLeft.contains(nums[k]) || surplusMediumRight.contains(nums[k])) {
continue;
}
int right = nums[k];
if (merge + right == 0) {
List<Integer> match = new ArrayList<>();
match.add(left);
match.add(medium);
match.add(right);
result.add(match);
}
surplusMediumRight.add(nums[k]);
}
surplusMediumRight.clear();
surplusLeftMedium.add(nums[j]);
}
surplusLeftMedium.clear();
surplusLeft.add(nums[i]);
}
return result;
}
}
*/
//下面是O(n^2)
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for(int i = 0; i < nums.length-2; i++) {
if(i == 0 || nums[i] != nums[i-1]) {
int lo = i+1;
int hi = nums.length-1;
int sum = 0 - nums[i];
while(lo < hi) {
if (nums[lo] + nums[hi] == sum) {
result.add(Arrays.asList(nums[i], nums[lo], nums[hi]));
while(lo < hi && nums[lo] == nums[lo+1]) lo++;
while(lo < hi && nums[hi] == nums[hi-1]) hi--;
lo++;
hi--;
}
else if(nums[lo] + nums[hi] < sum) {
lo++;
}
else {
hi--;
}
}
}
}
return result;
}
}