LeetCode 278 LintCode 74 – Python 3 (Week 10 – 04)

# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n == 0: return 0
        
        start, end = 1, n
        
        while start + 1 < end:
            mid = int(start + (end - start) / 2)
            print(mid)
            if isBadVersion(mid) == True:
                end = mid
            else:
                start = mid
            
            
        if isBadVersion(start) == True:
            return start
        else:
            return end
        

Leave a comment

Design a site like this with WordPress.com
Get started