Skip to content

Latest commit

 

History

History
40 lines (33 loc) · 853 Bytes

0278._first_bad_version.md

File metadata and controls

40 lines (33 loc) · 853 Bytes

Navigation

Links

  1. https://leetcode.com/problems/first-bad-version/
  2. https://leetcode-cn.com/problems/first-bad-version/

Solution 1 二分查找

因为有序

    时间复杂度:O(logN)
    空间复杂度:O(1)
# 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
        """
        lo, hi = 1, n
        
        while lo <= hi:
            mid = (lo + hi) // 2
            
            if isBadVersion(mid):
                hi = mid - 1
            else:
                lo = mid + 1
        
        return lo