forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0015-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
public class Solution {
public IList<IList<int>> ThreeSum(int[] nums) {
List<IList<int>> res = new List<IList<int>>();
int left, right;
Array.Sort(nums);
for(int i = 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--;
}else if(nums[i] + nums[left] + nums[right] < 0){
left++;
}else {
res.Add(new List<int> {nums[i], nums[left], nums[right]});
left++;
while(nums[left] == nums[left-1] && left < right){
left++;
}
}
}
}
return res;
}
}