-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
45 lines (44 loc) · 1.03 KB
/
Solution.cs
File metadata and controls
45 lines (44 loc) · 1.03 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
public class Solution
{
public int[] SearchRange(int[] nums, int target)
{
int[] ret = [-1, -1];
int n = nums.Length;
int low = 0, high = n - 1, ans = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (nums[mid] == target)
{
ans = mid;
high = mid - 1;
}
else if (nums[mid] > target)
{
high = mid - 1;
}
else low = mid + 1;
}
if (ans == -1) return ret;
ret[0] = ans;
low = 0;
high = n - 1;
ans = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (nums[mid] == target)
{
ans = mid;
low = mid + 1;
}
else if (nums[mid] < target)
{
low = mid + 1;
}
else high = mid - 1;
}
ret[1] = ans;
return ret;
}
}