-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchRange.c
88 lines (69 loc) · 1.19 KB
/
searchRange.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#include<stdlib.h>
int * searchRange(int *nums,int numsSize,int target, int* returnSize)
{
int *result = (int*) malloc(sizeof(int)* (*returnSize));
result[0] = -1;
result[1] = -1;
if(numsSize ==0) return result;
int index = bin_search(nums,0,numsSize-1,target);
printf("Index: %d\n",index);
//search start and end pos here;
if(index == -1)
{
return result;
}
int start;
int end;
int i=index;
while(i>=0 && nums[i] == target)
{
i--;
}
i=i+1;
start = i;
i=index;
while(i<numsSize && nums[i] == target)
{
i++;
}
i=i-1;
end = i;
result[0] = start;
result[1] = end;
return result;
}
int bin_search(int* nums, int left, int right,int target)
{
int mid = (left+right)/2;
while(left <=right)
{
if(nums[mid] < target)
{
left= mid+1;
}
else if(nums[mid] == target)
{
return mid;
}
else
{
right = mid-1;
}
mid = (left+right)/2;
}
return -1;
}
void main(void)
{
int array[2];
//[5, 7, 7, 8, 8, 10]
array[0] = 2;
array[1] = 2;
int numsSize = 2;
int target = 2;
int a = 2;
int* returnSize = &a;
int *result = searchRange(array, numsSize, target, returnSize);
printf("%d %d\n",result[0],result[1]);
}