-
Notifications
You must be signed in to change notification settings - Fork 2
/
0033-search-in-rotated-sorted-array.rs
46 lines (41 loc) · 1.4 KB
/
0033-search-in-rotated-sorted-array.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 {
if nums.is_empty() {
return -1;
}
Self::search_rotated(&nums, target, 0_usize, nums.len() - 1)
}
fn search_rotated(nums: &Vec<i32>, target: i32, low: usize, high: usize) -> i32 {
if low > high {
return -1;
}
let mid = low + (high - low) / 2;
if nums[mid] == target {
return mid as i32;
}
if nums[low] <= nums[mid] {
// if left half is sorted
if nums[low] <= target && nums[mid] >= target {
// if target in left half of left half
Self::search_rotated(nums, target, low, mid - 1)
} else {
// search in right of left half
Self::search_rotated(nums, target, mid + 1, high)
}
} else {
// if right half is sorted
if nums[mid] <= target && nums[high] >= target {
// if target in right half
Self::search_rotated(nums, target, mid + 1, high)
} else {
// search left of right half
Self::search_rotated(nums, target, low, mid - 1)
}
}
}
}
fn main() {
assert_eq!(4, Solution::search(vec![4, 5, 6, 7, 0, 1, 2], 0));
assert_eq!(-1, Solution::search(vec![4, 5, 6, 7, 0, 1, 2], 3));
}