-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path34. Search for a Range.cs
43 lines (40 loc) · 1.2 KB
/
34. Search for a Range.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
43
public class Solution {
public int[] SearchRange(int[] nums, int target) {
int index = Array.BinarySearch(nums,target);
int upperIndex = 0, loweIndex = 0;
if(index<0)
{
//upperIndex = -1;
// loweIndex = -1;
int[] res={-1,-1};
return res;
}
else
{
upperIndex = index;
loweIndex = index;
while(true)
{
if (index == 0)
break;
index--;
if (nums[index] == target)
loweIndex = index;
else
break;
}
while (true)
{
if (index == nums.Length-1)
break;
index++;
if (nums[index] == target)
upperIndex = index;
else
break;
}
int[] res={loweIndex,upperIndex};
return res;
}
}
}