-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15. 3Sum
43 lines (43 loc) · 1.1 KB
/
15. 3Sum
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
class Solution
{
public List<List<Integer>> threeSum(int[] nums)
{
ArrayList<List<Integer>> l = new ArrayList<List<Integer>> ();
if(nums.length<=2)
return l;
Arrays.sort(nums);
if(nums[0]>0)
return l;
int i,j,k,c=nums.length;
for(i=0;i<c;i++)
{
j=i+1;
k=c-1;
if(i>0 && nums[i]==nums[i-1])
continue;
while(j<k)
{
if(k<c-1 && nums[k]==nums[k+1])
{
k--;
continue;
}
if(nums[i]+nums[j]+nums[k]>0)
k--;
else if(nums[i]+nums[j]+nums[k]<0)
j++;
else
{
List<Integer> x= new ArrayList<Integer>();
x.add(nums[i]);
x.add(nums[j]);
x.add(nums[k]);
l.add(x);
j++;
k--;
}
}
}
return l;
}
}