-
Notifications
You must be signed in to change notification settings - Fork 0
/
s0374_guess_number_higher_or_lower.rs
54 lines (50 loc) · 1.25 KB
/
s0374_guess_number_higher_or_lower.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
struct Solution;
static mut X: i32 = 0;
unsafe fn guess(num: i32) -> i32 {
use std::cmp::Ordering::*;
match X.cmp(&num) {
Equal => 0,
Greater => 1,
Less => -1,
}
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is lower than the guess number
* 1 if num is higher than the guess number
* otherwise return 0
* unsafe fn guess(num: i32) -> i32 {}
*/
impl Solution {
#[allow(non_snake_case)]
unsafe fn guessNumber(n: i32) -> i32 {
let mut left = 1;
let mut right = n;
while left <= right {
let mid = left + ((right - left) >> 1);
match guess(mid) {
-1 => right = mid,
1 => left = mid + 1,
0 => return mid,
_ => {}
}
}
1
}
}
//leetcode submit region end(Prohibit modification and deletion)
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
unsafe {
// X = 6;
// assert_eq!(Solution::guessNumber(10), 6);
X = 2;
assert_eq!(Solution::guessNumber(2), 2);
}
}
}