-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeSum.java
More file actions
61 lines (54 loc) · 1.7 KB
/
ThreeSum.java
File metadata and controls
61 lines (54 loc) · 1.7 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
package com.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by jamylu on 2018/1/3.
* leetcode015.
* Given an array S of n integers.Find all unique triplets in the array which gives the sum of zero.
*/
public class ThreeSum {
public static void main(String[] args) {
int[] nums = {0, 0, 0};
System.out.println(threeS(nums));
}
public static List<List<Integer>> threeS(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if (nums.length <= 2 || nums == null) {
return list;
}
//先排序
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
// 出现重复元素
if (i - 1 > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
//双指针
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
list.add(Arrays.asList(nums[i], nums[left], nums[right]));
//出现重复则一直移动
while (left < right - 1 && nums[left] == nums[left + 1]) {
left++;
}
while (right > left + 1 && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
}
if (sum < 0) {
left++;
}
if (sum > 0) {
right--;
}
}
}
return list;
}
}