-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search_ranges.c
111 lines (89 loc) · 2.61 KB
/
binary_search_ranges.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdlib.h>
int binary_search_left(int *nums, int size, int target);
int binary_search_right(int *nums, int size, int target);
int* searchRange(int* nums, int numsSize, int target, int* returnSize);
int main(int argc, char **argv) {
int nums[] = { 5, 7, 7, 8, 8, 10 };
int size = sizeof(nums) / sizeof(nums[0]);
int *res;
int res_size; // ignore
res = searchRange(nums, size, 8, &res_size);
printf("\n[%d, %d]", res[0], res[1]);
free(res);
res = searchRange(nums, size, 6, &res_size);
printf("\n[%d, %d]", res[0], res[1]);
free(res);
res = searchRange(nums, size, 0, &res_size);
printf("\n[%d, %d]", res[0], res[1]);
free(res);
int nums1[] = {};
int size1 = sizeof(nums1) / sizeof(nums1[0]);
res = searchRange(nums1, size1, 0, &res_size);
printf("\n[%d, %d]", res[0], res[1]);
free(res);
int nums2[] = { 1 };
int size2 = sizeof(nums2) / sizeof(nums2[0]);
res = searchRange(nums2, size2, 1, &res_size);
printf("\n[%d, %d]", res[0], res[1]);
free(res);
int nums3[] = { 2, 2 };
int size3 = sizeof(nums3) / sizeof(nums3[0]);
res = searchRange(nums3, size3, 3, &res_size);
printf("\n[%d, %d]", res[0], res[1]);
free(res);
res = searchRange(nums3, size3, 2, &res_size);
printf("\n[%d, %d]", res[0], res[1]);
free(res);
printf("\n");
return EXIT_SUCCESS;
}
/**
* https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
* Note: The returned array must be malloced, assume caller calls free().
*/
int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
*returnSize = 2;
int *res = malloc(sizeof(int) * (*returnSize));
res[0] = -1;
res[1] = -1;
res[0] = binary_search_left(nums, numsSize, target);
if (res[0] == -1) {
res[1] = -1;
} else {
res[1] = binary_search_right(nums, numsSize, target);
}
return res;
}
int binary_search_left(int *nums, int size, int target) {
int l = 0, r = size;
if (size < 1) {
return -1;
}
while (l < r) {
int m = l + ((r - l) / 2);
if (nums[m] < target) {
l = m + 1;
} else {
r = m;
}
}
return nums[l] == target ? l : -1;
}
int binary_search_right(int *nums, int size, int target) {
int l = 0, r = size;
if (size < 1) {
return -1;
}
while (l < r) {
int m = l + ((r - l) / 2);
if (target < nums[m]) {
r = m;
} else {
l = m + 1;
}
}
if (nums[l - 1] == target)
return l - 1;
return -1;
}