Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f70862c

Browse files
Merge pull request #17 from CarlosZoft/add704
feat: added solution for problem 704
2 parents dfecd2a + 15eb04a commit f70862c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎0704_binary_search/binary_search.c‎

Lines changed: 23 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /