- https://leetcode.com/problems/first-bad-version/
- https://leetcode-cn.com/problems/first-bad-version/
因为有序
时间复杂度: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