python binary search


  • 0

    def firstBadVersion(self, n):

        if n == 1:
            return 1
        
        #calculate the midpoint
        left = 0
        right = n
        mid = (right + left) // 2
        
        
        while left < right:
            if isBadVersion(mid) == True:
                right = mid
                mid = (right + left) // 2
            elif isBadVersion(mid) == False:
                left = mid
                mid = (right + left) // 2
                
            if left == right - 1:
                return right
            
        return right

Log in to reply
 

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