You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
template binary search, need to consider if return left or right.
classSolution:
deffirstBadVersion(self, n):
""" :type n: int :rtype: int """left, right=0, n-1while(left<=right):
mid=left+ (right-left) //2# if true, on the right side.if(isBadVersion(mid)):
right=mid-1else:
left=mid+1returnleft
Algorithm 2
use a variable to record the last true element.
classSolution:
deffirstBadVersion(self, n):
ans=-1left, right=1, nwhile(left<=right):
mid=left+ (right-left) //2print(mid)
# if true, on the right side.if(isBadVersion(mid)):
ans=midright=mid-1else:
left=mid+1returnans