-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
63 lines (54 loc) · 2.01 KB
/
main.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
fn main() {
assert_eq!(Solution::search(vec![2,5,6,0,0,1,2], 0), true);
}
struct Solution {}
impl Solution {
/// I just wonder why i use recursive ? two complex
pub fn search(nums: Vec<i32>, target: i32) -> bool {
Self::s(&nums, target, 0, nums.len())
}
pub fn s(nums: & Vec<i32>, target: i32, start: usize, end: usize) -> bool {
if start == end { return false }
if start + 1 == end { return target == nums[start] }
if start + 2 == end { return nums[start] == target || nums[start + 1] == target }
let m = (start + end - 1) / 2;
if nums[start] == nums[end - 1] {
return Self::s(nums, target, start + 1, end);
}
if nums[start] < nums[end - 1] {
if nums[m] == target { return true }
if nums[m] < target { return Self::s(nums, target, m+1, end) }
if nums[m] > target { return Self::s(nums, target, start, m) }
}
if nums[start] > nums[end - 1] {
if target == nums[m] { return true }
if nums[start] <= nums[m] {
if nums[start] <= target && target <= nums[m] {
return Self::s(nums, target, start, m)
} else { return Self::s(nums, target, m+1, end) }
}
else {
if nums[m] <= target && target <= nums[end - 1] {
return Self::s(nums, target, m+1, end)
} else { return Self::s(nums, target, start, m) }
}
}
unreachable!()
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::search(vec![2,5,6,0,0,1,2], 0), true);
assert_eq!(Solution::search(vec![2,5,6,0,0,1,2], 3), false);
assert_eq!(Solution::search(vec![1,2,1], 1), true);
assert_eq!(Solution::search(vec![3,1,1], 3), true);
// assert_eq!(Solution::search(vec![3,1,1,1,1], 3), true);
}
#[test]
fn fail() {
assert_eq!(Solution::search(vec![3,1,1,1,1], 3), true);
}
}