-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbinary-search.rs
54 lines (52 loc) · 1.51 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
// 704. Binary Search
// 🟢 Easy
//
// https://leetcode.com/problems/binary-search/
//
// Tags: Array - Binary Search
struct Solution;
impl Solution {
/// Use binary search! Use a left and right pointer, at each iteration
/// compute the mid-point between them, when the value at that mid index is
/// greater than the target, we know that the target will be to the left if
/// it found in the array, when the value is lesser, it will be found to
/// the right.
///
/// Time complexity: O(log(n)) - Each iteration discards half of the
/// search space.
/// Space complexity: O(1) - We use constant space.
///
/// Runtime 5 ms Beats 66.86%
/// Memory 2.2 MB Beats 79.66%
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
let (mut l, mut r) = (0, nums.len() - 1);
let mut mid;
while l < r {
// mid = (l+r) / 2; works as well, 10^4 won't overflow.
mid = l + (r - l) / 2;
if nums[mid] < target {
l = mid + 1;
} else {
r = mid;
}
}
if nums[l] == target {
l as i32
} else {
-1
}
}
}
// Tests.
fn main() {
let tests = [
(vec![5], 5, 0),
(vec![-4, -2, 0, 1], 2, -1),
(vec![-1, 0, 3, 5, 9, 12], 9, 4),
(vec![-1, 0, 3, 5, 9, 12], 2, -1),
];
for t in tests {
assert_eq!(Solution::search(t.0.clone(), t.1), t.2);
}
println!("\x1b[92m» All tests passed!\x1b[0m")
}