-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search.rs
46 lines (41 loc) · 1.37 KB
/
binary-search.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
struct Solution;
impl Solution {
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
use std::cmp::Ordering;
let (mut left, mut right) = (0, nums.len() - 1);
let mut mid;
while left <= right {
mid = left + (right - left) / 2;
match nums[mid].cmp(&target) {
Ordering::Equal => return mid as i32,
Ordering::Less => left += 1,
Ordering::Greater => {
if right == 0 {
break;
}
right -= 1;
}
}
}
-1
}
}
fn main() {
println!("{}", Solution::search(vec![-1, 0, 3, 5, 9, 12], 9));
println!("{}", Solution::search(vec![-1, 0, 3, 5, 9, 12], 10));
println!("{}", Solution::search(vec![-1, 0, 3, 5, 9, 12], 12));
println!("{}", Solution::search(vec![1, 0, 3, 5, 9, 12], 2));
println!("{}", Solution::search(vec![5], -5));
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert_eq!(Solution::search(vec![-1, 0, 3, 5, 9, 12], 9), 4);
assert_eq!(Solution::search(vec![-1, 0, 3, 5, 9, 12], 10), -1);
assert_eq!(Solution::search(vec![-1, 0, 3, 5, 9, 12], 12), 5);
assert_eq!(Solution::search(vec![1, 0, 3, 5, 9, 12], 2), -1);
assert_eq!(Solution::search(vec![5], -5), -1);
}
}