-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.rs
89 lines (80 loc) · 2.22 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/// Given an array of integers `nums` which is sorted in ascending order, and
/// an integer `target`, write a function to search `target` in `nums`. If
/// `target` exists, then return its index. Otherwise, return `-1`.
///
/// You must write an algorithm with `O(log n)` runtime complexity.
struct Solution;
impl Solution {
fn search_range(nums: &Vec<i32>, target: i32, start: usize, end: usize) -> i32 {
let mid = start + (end - start) / 2;
let value = nums[mid];
if value == target {
mid as i32
} else if value > target {
if mid == start {
-1
} else {
Self::search_range(nums, target, start, mid - 1)
}
} else {
if mid == end {
-1
} else {
Self::search_range(nums, target, mid + 1, end)
}
}
}
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
if nums.len() > 0 {
Self::search_range(&nums, target, 0, nums.len() - 1)
} else {
-1
}
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let nums = vec![-1, 0, 3, 5, 9, 12];
let target = 9;
let result = Solution::search(nums, target);
assert_eq!(result, 4);
}
#[test]
fn example_2() {
let nums = vec![-1, 0, 3, 5, 9, 12];
let target = 2;
let result = Solution::search(nums, target);
assert_eq!(result, -1);
}
#[test]
fn beginning() {
let nums = vec![-1, 0, 3, 5, 9, 12];
let target = -1;
let result = Solution::search(nums, target);
assert_eq!(result, 0);
}
#[test]
fn end() {
let nums = vec![-1, 0, 3, 5, 9, 12];
let target = 12;
let result = Solution::search(nums, target);
assert_eq!(result, 5);
}
#[test]
fn only_yes() {
let nums = vec![1];
let target = 1;
let result = Solution::search(nums, target);
assert_eq!(result, 0);
}
#[test]
fn only_no() {
let nums = vec![1];
let target = -1;
let result = Solution::search(nums, target);
assert_eq!(result, -1);
}
}