Skip to content

Commit 7494315

Browse files
committed
Runtime: 3 ms (Top 74.71%) | Memory: 2.20 MB (Top 87.65%)
1 parent 7b200de commit 7494315

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Runtime: 3 ms (Top 74.71%) | Memory: 2.20 MB (Top 87.65%)
2+
3+
use std::cmp::Ordering;
4+
5+
impl Solution {
6+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
7+
let mut min: i32 = 0;
8+
let mut max = (nums.len() - 1) as i32; // use i32 to be able to go under zero
9+
10+
while min <= max {
11+
let mid = (min + max) / 2;
12+
match nums[mid as usize].cmp(&target) {
13+
Ordering::Equal => return mid,
14+
Ordering::Less => min = mid + 1,
15+
Ordering::Greater => max = mid - 1,
16+
}
17+
}
18+
return -1;
19+
}
20+
}

0 commit comments

Comments
 (0)