90%+ python solution


  • 0
    A

    Simple binary search, be careful on the exit condition

    class Solution(object):
        def guessNumber(self, n):
            """
            :type n: int
            :rtype: int
            """
            left = 1
            right = n
            while left <= right:
                mid = (left + right) >> 1
                g = guess(mid)
                if g == 0:
                    return mid
                elif g == 1:
                    if left == mid:
                        return left+1
                    left = mid
                else:
                    right = mid
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.