Skip to content

Commit 832a222

Browse files
authored
Create Leetcode 704. Binary Search.java
1 parent aea7c5b commit 832a222

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int search(int[] nums, int target) {
3+
int start = 0;
4+
int end = nums.length - 1;
5+
6+
int mid = start + (end - start)/2;
7+
8+
while(start <= end){
9+
if(nums[mid] == target){
10+
return mid;
11+
}
12+
if(nums[mid] > target){
13+
end = mid - 1;
14+
} else {
15+
start = mid + 1;
16+
}
17+
mid = start + (end - start)/2;
18+
}
19+
return -1;
20+
}
21+
}

0 commit comments

Comments
 (0)