-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path034-SearchForARange.cs
42 lines (38 loc) · 1.16 KB
/
034-SearchForARange.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
32
33
34
35
36
37
38
39
40
41
42
//-----------------------------------------------------------------------------
// Runtime: 240ms
// Memory Usage: 32 MB
// Link: https://leetcode.com/submissions/detail/379089611/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _034_SearchForARange
{
public int[] SearchRange(int[] nums, int target)
{
int lo = 0, hi = nums.Length - 1, mid;
while (lo <= hi)
{
mid = lo + (hi - lo) / 2;
if (target > nums[mid])
lo = mid + 1;
else
hi = mid - 1;
}
if (lo == nums.Length || nums[lo] != target)
return new int[] { -1, -1 };
var result = new int[2];
result[0] = lo;
lo = 0; hi = nums.Length - 1;
while (lo <= hi)
{
mid = lo + (hi - lo) / 2;
if (target >= nums[mid])
lo = mid + 1;
else
hi = mid - 1;
}
result[1] = lo - 1;
return result;
}
}
}