-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3Sum.java
More file actions
34 lines (33 loc) · 969 Bytes
/
3Sum.java
File metadata and controls
34 lines (33 loc) · 969 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);
HashSet<List<Integer>> set= new HashSet<>();
for(int i=0;i<nums.length;i++)
{
int left=i+1,right=nums.length-1;
while(left<right)
{
if(nums[i]+nums[left]+nums[right]==0)
{
ArrayList<Integer> temp=new ArrayList<>();
temp.add(nums[i]);
temp.add(nums[left]);
temp.add(nums[right]);
set.add(temp);
left++;
right--;
}
else if(nums[left]+nums[right]+nums[i]>0)
right--;
else
left++;
}
}
List<List<Integer>> re= new ArrayList<>();
for(List tt : set)
{
re.add(tt);
}
return re;
}
}