-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path3Sum
More file actions
35 lines (27 loc) · 1.2 KB
/
3Sum
File metadata and controls
35 lines (27 loc) · 1.2 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
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result=new ArrayList<>();
for(int i1=0;i1+2<nums.length;i1++)
{
if(i1>0 && nums[i1]==nums[i1-1])
continue;
int i2=i1+1;
int i3=nums.length-1;
while(i2<i3)
{
int sum=nums[i1]+nums[i2]+nums[i3];
if(sum==0)
{
result.add(Arrays.asList(nums[i1],nums[i2],nums[i3]));
i3--;
while( i3>i2 && nums[i3]==nums[i3+1])
i3--;
}
if(sum<0)
i2++;
if(sum>0)
i3--;
}
}
return result;
}