Repository navigation
Expand file tree
/
Copy pathTwoSumIIInputArrayIsSorted.cs
More file actions
81 lines (67 loc) · 2.07 KB
/
Copy pathTwoSumIIInputArrayIsSorted.cs
File metadata and controls
81 lines (67 loc) · 2.07 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace Solutions.Problems;
public class TwoSumIIInputArrayIsSortedSolution
{
public int[] TwoSumBruteForce(int[] numbers, int target)
{
for (int i = 0; i < numbers.Length; i++)
for (int j = i + 1; j < numbers.Length; j++)
if (numbers[i] + numbers[j] == target)
return [i + 1, j + 1];
else if (numbers[i] + numbers[j] > target)
break;
return [];
}
public int[] TwoSumBinarySearch(int[] numbers, int target)
{
for (int i = 0; i < numbers.Length; i++)
{
(int left, int right) = (i + 1, numbers.Length - 1);
while (left <= right)
{
int mid = left + (right - left) / 2;
switch (numbers[mid].CompareTo(target - numbers[i]))
{
case < 0:
left = mid + 1;
break;
case > 0:
right = mid - 1;
break;
case 0:
return [i + 1, mid + 1];
}
}
}
return [];
}
public int[] TwoSumHashMap(int[] numbers, int target)
{
Dictionary<int, int> hashMap = [];
foreach ((int num, int right) in numbers.Select((n, i) => (n, i)))
{
if (hashMap.TryGetValue(target - num, out int left))
return [left + 1, right + 1];
hashMap[num] = right;
}
return [];
}
public int[] TwoSumTwoPointers(int[] numbers, int target)
{
(int left, int right) = (0, numbers.Length - 1);
while (left < right)
{
switch ((numbers[left] + numbers[right]).CompareTo(target))
{
case < 0:
left++;
break;
case > 0:
right--;
break;
case 0:
return [left + 1, right + 1];
}
}
return [];
}
}