-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp034.c
61 lines (61 loc) · 1.27 KB
/
p034.c
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
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
int *result = (int*)malloc(sizeof(int)*2);
*returnSize = 2;
unsigned l = 0;
unsigned r = numsSize;
if (!r)
{
result[0] = result[1] = -1;
return result;
}
unsigned rr = r;
unsigned m;
while (r-l > 1)
{
m = (r+l)>>1;
if (nums[m] < target)
{
l = m;
}
else
{
r = m;
if (nums[m] > target) rr = m;
}
}
result[0] = l;
if (nums[result[0]] < target)
{
result[0]++;
if (result[0] >= numsSize || nums[result[0]] != target)
{
result[0] = result[1] = -1;
return result;
}
}
else if (nums[result[0]] > target)
{
result[0] = result[1] = -1;
return result;
}
l = result[0];
r = rr;
while (r-l > 1)
{
m = (r+l)>>1;
if (nums[m] > target)
{
r = m;
}
else
{
l = m;
}
}
result[1] = l;
return result;
}