Skip to content

Commit fc5ea09

Browse files
authored
Merge pull request neetcode-gh#1985 from nirajvenkat/patch-4
Create 0704-binary-search.rs
2 parents a82fd10 + 034bd4c commit fc5ea09

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

rust/0704-binary-search.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::cmp::Ordering::{Equal, Less, Greater};
2+
3+
impl Solution {
4+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
5+
let (mut l, mut r) = (0, nums.len());
6+
7+
while l < r {
8+
let m = l + (r - l) / 2;
9+
match target.cmp(&nums[m]) {
10+
Equal => return m as i32,
11+
Less => r = m,
12+
Greater => l = m + 1,
13+
}
14+
}
15+
16+
-1
17+
}
18+
}

0 commit comments

Comments
 (0)