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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy