We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7b200de commit 7494315Copy full SHA for 7494315
scripts/algorithms/B/Binary Search/Binary Search.rs
@@ -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