Python solution using binary search


  • 1
    A
    # The guess API is already defined for you.
    # @param num, your guess
    # @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
    # def guess(num):
    
    class Solution(object):
        def guessNumber(self, n):
            """
            :type n: int
            :rtype: int
            """
            low = 1
            high = n
            while low <= high:
                mid = (low + high)//2
                res =  guess(mid)
                if res == 0 :
                    return mid
                elif res == -1:
                    high = mid - 1
                else:
                    low = mid + 1
                    
    

Log in to reply
 

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