/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
//二分查找的细节,1.注意计算mid时越界,2.由于时间限制,接口只能少调用,使用二分查找的最左侧值的写法
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int low = 1;
int high = n+1;//表示[)
while (low < high) {//循环结束条件为low==high,此时搜索区间为[low,low)
int mid = low + ((high - low) >> 1);
if(isBadVersion(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
}
二分法的三种写法以及细节参照https://www.cnblogs.com/kyoner/p/11080078.html
关于移位操作的讲解https://blog.csdn.net/weixin_39549852/article/details/110724142