Easy binary search solution in Python


  • 0
    A
    class Solution(object):
        def guessNumber(self, n):
            """
            :type n: int
            :rtype: int
            """
            def helper(lo, hi):
                if guess(lo) == 0:
                    return lo
                if guess(hi) == 0:
                    return hi
                mid = lo + (hi-lo) // 2
                if guess(mid) == 0:
                    return mid
                elif guess(mid) == -1:
                    return helper(lo, mid-1)
                else:
                    return helper(mid+1, hi)
            return helper(1,n)
    

Log in to reply
 

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