forked from habibullah235/Hacktober-asian
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.java
More file actions
34 lines (29 loc) · 917 Bytes
/
3Sum.java
File metadata and controls
34 lines (29 loc) · 917 Bytes
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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
int len = nums.length;
Set<List<Integer>> set = new HashSet<>();
for(int i=0;i<nums.length-1;i++) {
if(i!=0)
if(nums[i]==nums[i-1])
continue;
int j = i+1;
int k = len-1;
while(j<k) {
int val = nums[i]+nums[j]+nums[k];
//if(nums[j-1]==nums[j] && nums[k-1]==nums[k])
// continue;
if(val == 0) {
set.add(Arrays.asList(nums[i], nums[j], nums[k]));
--k;
++j;
}
else if(val<0)
++j;
else
--k;
}
}
return new ArrayList<>(set);
}
}