forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0167-TwoSumII.cs
More file actions
25 lines (23 loc) · 772 Bytes
/
0167-TwoSumII.cs
File metadata and controls
25 lines (23 loc) · 772 Bytes
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
//-----------------------------------------------------------------------------
// Runtime: 244ms
// Memory Usage: 31.1 MB
// Link: https://leetcode.com/submissions/detail/343620812/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0167_TwoSumII
{
public int[] TwoSum(int[] numbers, int target)
{
int i = 0, j = numbers.Length - 1;
while (i < j)
{
int compare = (numbers[i] + numbers[j]).CompareTo(target);
if (compare == 0) return new int[] { i + 1, j + 1 };
else if (compare > 0) j--;
else if (compare < 0) i++;
}
return new int[] { };
}
}
}