-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0015.java
More file actions
38 lines (34 loc) · 1.1 KB
/
Copy pathLeetCode0015.java
File metadata and controls
38 lines (34 loc) · 1.1 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
/* 3Sum
* Example:
* Given array nums = [-1, 0, 1, 2, -1, -4],
* A solution set is:[ [-1, 0, 1], [-1, -1, 2] ]
* */
package Array;
import java.util.*;
public class LeetCode0015 {
public static void main(String args[]) {
int[] nums = {-1, 0, 1, 2, -1, -4};
System.out.println(threeSum(nums));
}
public static List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> res = new HashSet(); //set is to avoid duplicates
if (nums.length < 3)
return new ArrayList(res);
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
int sum = nums[j] + nums[k];
if (nums[i] + sum == 0) {
res.add(Arrays.asList(nums[i], nums[j++], nums[k--]));
} else if (nums[i] + sum > 0) {
k--;
} else if (nums[i] + sum < 0) {
j++;
}
}
}
return new ArrayList(res);
}
}