Skip to content

Commit 15eb04a

Browse files
committed
feat: added solution for problem 704
1 parent dfecd2a commit 15eb04a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

0704_binary_search/binary_search.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
int search(int* nums, int numsSize, int target){
4+
if(target > nums[numsSize-1] || target < nums[0])return -1;
5+
int begin = -1 ,end = numsSize;
6+
while(begin < end-1){
7+
int half = (begin+end)/2;
8+
if(nums[half]<target)begin = half;
9+
else end = half;
10+
}
11+
if(nums[end]==target) return end ;
12+
else return -1;
13+
}
14+
15+
int main(void) {
16+
int test[6] = {-1,0,3,5,9,12};
17+
18+
//test 1 - number 2;
19+
printf("possible index = %d\n",search(test,6,2));
20+
//test 2 - number 9
21+
printf("possible index = %d\n",search(test,6,9));
22+
return 0;
23+
}

0 commit comments

Comments
 (0)