题目:
278.第一个错误的版本(Easy)
代码:
# 常用的 左闭右开,二分法,要当心判断条件 到底是True还是False
# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:
class Solution:
def firstBadVersion(self, n: int) -> int:
# 用 左闭右开二分法来做;
left,right = 0,n
while left < right:
mid = left + (right - left)//2
if isBadVersion(mid) == False : # 向右搜索,左闭右开,参考左闭左开,left要+1
left = mid + 1
else : # 向左搜索,右开不参考右闭,所以不-1
right = mid
return left