374. Guess Number Higher or Lower Easy
1/**
2 * [374] Guess Number Higher or Lower
3 *
4 * We are playing the Guess Game. The game is as follows:
5 * I pick a number from 1 to n. You have to guess which number I picked.
6 * Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
7 * You call a pre-defined API int guess(int num), which returns three possible results:
8 *
9 * -1: Your guess is higher than the number I picked (i.e. num > pick).
10 * 1: Your guess is lower than the number I picked (i.e. num < pick).
11 * 0: your guess is equal to the number I picked (i.e. num == pick).
12 *
13 * Return the number that I picked.
14 *
15 * Example 1:
16 *
17 * Input: n = 10, pick = 6
18 * Output: 6
19 *
20 * Example 2:
21 *
22 * Input: n = 1, pick = 1
23 * Output: 1
24 *
25 * Example 3:
26 *
27 * Input: n = 2, pick = 1
28 * Output: 1
29 *
30 *
31 * Constraints:
32 *
33 * 1 <= n <= 2^31 - 1
34 * 1 <= pick <= n
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/guess-number-higher-or-lower/
40// discuss: https://leetcode.com/problems/guess-number-higher-or-lower/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44/**
45 * Forward declaration of guess API.
46 * @param num your guess
47 * @return -1 if num is higher than the picked number
48 * 1 if num is lower than the picked number
49 * otherwise return 0
50 * unsafe fn guess(num: i32) -> i32 {}
51 */
52
53impl Solution {
54 unsafe fn guessNumber(n: i32) -> i32 {
55 0
56 }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_374() {
67 }
68}
69
Back
© 2025 bowen.ge All Rights Reserved.