LeetCode374(2021.6.14)

LC374. 猜数字大小

/** 
 * 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
 * int guess(int num);
 */

class Solution {
public:
    int guessNumber(int n) {
        long long l = 1, r = n;
        while (l < r) {
            long long mid = l + r >> 1;
            if (guess(mid) <= 0) r = mid;
            else l = mid + 1;
        }
        return l;
    }
};

发表评论