Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1985 from nirajvenkat/patch-4
Browse files Browse the repository at this point in the history
Create 0704-binary-search.rs
  • Loading branch information
tahsintunan authored Jan 11, 2023
2 parents a82fd10 + 034bd4c commit fc5ea09
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions rust/0704-binary-search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::cmp::Ordering::{Equal, Less, Greater};

impl Solution {
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
let (mut l, mut r) = (0, nums.len());

while l < r {
let m = l + (r - l) / 2;
match target.cmp(&nums[m]) {
Equal => return m as i32,
Less => r = m,
Greater => l = m + 1,
}
}

-1
}
}

0 comments on commit fc5ea09

Please sign in to comment.