-
Notifications
You must be signed in to change notification settings - Fork 0
/
Three3Sum.java
54 lines (45 loc) · 1.32 KB
/
Three3Sum.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
package LeetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.text.html.HTMLDocument.Iterator;
public class Three3Sum {
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> resList = new ArrayList<List<Integer>>();
Set<List<Integer>> res = new HashSet<List<Integer>>();
int len = nums.length,first , second, third;
Arrays.sort(nums);
for(int i=0;i<len;i++){
first = i;
second = i+1;
third = len-1;
while(second < third){
if(nums[first] + nums[second] + nums[third] == 0){
List<Integer> temp = new ArrayList<Integer>();
temp.add(nums[first]);
temp.add(nums[second]);
temp.add(nums[third]);
res.add(temp);
second++;
third--;
}
else if(nums[first] + nums[second] + nums[third] < 0)
second++;
else if(nums[first] + nums[second] + nums[third] > 0)
third--;
}
}
resList.addAll(res);
return resList;
}
public static void main(String[] args) {
int[] arr = {-1, 0, 1, 2, -1, -4};
List<List<Integer>> res = threeSum(arr);
for(int i=0;i<res.size();i++){
System.out.println(res.get(i).toString());
}
}
}