lc278. 第一个错误的版本

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

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        # 二分查找的变种题
        begin_version, end_version = 1, n
        is_find = False
        result = 1
        while(not is_find):
            middle = int((begin_version + end_version) / 2)
            if middle == begin_version:
                    is_find = True
                    result = middle + 1   
            if isBadVersion(middle):
                if not isBadVersion(middle - 1):
                    is_find = True
                    result = middle
                else:
                    end_version = middle
            else:
                begin_version = middle
        return result        

  

posted @ 2022-04-27 19:01  stronger_el  阅读(7)  评论(0)    收藏  举报