文章目录
1. 题目
2. 思路
(1) 二分查找
- 注意!查找分界线时终止条件的判断符号是<,根据需要,最终left=right=分界线的左侧或右侧。
3. 代码
public class Test {
public static void main(String[] args) {
}
}
class VersionControl {
boolean isBadVersion(int version) {
return true;
}
}
class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 1;
int right = n;
int mid;
while (left < right) {
mid = left + ((right - left) >>> 1);
if (isBadVersion(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return right;
}
}