Skip to content

Commit 48f6c9a

Browse files
authored
Create 704-Binary-Search.java
Add solution for 704 https://leetcode.com/problems/binary-search/
1 parent b80f151 commit 48f6c9a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

java/704-Binary-Search.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int search(int[] nums, int target) {
3+
4+
int i = 0;
5+
int j = nums.length-1;
6+
7+
while(i<=j){
8+
int mid = (i+j)/2;
9+
10+
if(nums[mid] == target) return mid;
11+
else if(nums[mid]<target) i = mid+1;
12+
else j = mid-1;
13+
}
14+
return -1;
15+
}
16+
}

0 commit comments

Comments
 (0)