题目
简单来说,可以看作是有序数列。只不过只有两种值,True/Fasle。
目的就是找到第一个True,那么二分查找挺适合的。
我的代码(二分查找)
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
low=1
high=n
first=n
while high>=low:
mid =low +(high-low)//2
if isBadVersion(mid):
first=mid
high=mid-1
else:
low=mid+1
return first