forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0015-3sum.cs
31 lines (28 loc) · 971 Bytes
/
0015-3sum.cs
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
publicclassSolution{
publicIList<IList<int>>ThreeSum(int[]nums){
List<IList<int>>res=newList<IList<int>>();
intleft,right;
Array.Sort(nums);
for(inti=0;i<nums.Length;i++){
if(i>0&&nums[i]==nums[i-1]){
continue;
}
left=i+1;
right=nums.Length-1;
while(left<right){
if(nums[i]+nums[left]+nums[right]>0){
right--;
}elseif(nums[i]+nums[left]+nums[right]<0){
left++;
}else{
res.Add(newList<int>{nums[i],nums[left],nums[right]});
left++;
while(nums[left]==nums[left-1]&&left<right){
left++;
}
}
}
}
returnres;
}
}