Skip to content

374 Guess Number Higher or Lower

374. Guess Number Higher or Lower

题目: https://leetcode.com/problems/guess-number-higher-or-lower/

难度:

Easy

思路

二分

class Solution(object):
    def guessNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        l, r = 1, n
        while l <= r:
            mid = l + ((r - l) >> 2)
            if guess(mid) == 1:
                l = mid + 1
            elif guess(mid) == -1:
                r = mid - 1
            else:
                return mid


回到顶部