generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem4.cs
More file actions
58 lines (51 loc) · 1.67 KB
/
Problem4.cs
File metadata and controls
58 lines (51 loc) · 1.67 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/3sum/">3Sum</see>.
/// </summary>
public static class Problem4
{
/// <summary>
/// Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
/// Time complexity: O(n^2).
/// Space complexity: O(n).
/// </summary>
/// <param name="nums">Array to traverse.</param>
/// <returns>Triplets that add up to 0.</returns>
public static IList<IList<int>> ThreeSum(int[] nums)
{
var result = new List<IList<int>>();
Array.Sort(nums);
for (int i = 0, length = nums.Length - 2; i < length; i++)
{
if (i == 0 || nums[i] != nums[i - 1])
{
TwoSum(nums, -nums[i], i + 1, nums.Length - 1, result);
}
}
return result;
}
private static void TwoSum(int[] nums, int targetSum, int leftStart, int rightStart, IList<IList<int>> result)
{
for (int left = leftStart, right = rightStart; left < right;)
{
var sum = nums[left] + nums[right];
if (sum < targetSum)
{
left++;
}
else if (sum > targetSum)
{
right--;
}
else
{
if (left == leftStart || right == rightStart || nums[left] != nums[left - 1] || nums[right] != nums[right + 1])
{
result.Add(new List<int> { -targetSum, nums[left], nums[right] });
}
left++;
right--;
}
}
}
}