leet-code/src/main/java/leetcode/editor/cn/FirstBadVersion.java
2021-06-13 15:02:33 +08:00

60 lines
2.0 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有
//版本都是错的。
//
// 假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。
//
// 你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误
//的版本。你应该尽量减少对调用 API 的次数。
//
// 示例:
//
// 给定 n = 5并且 version = 4 是第一个错误的版本。
//
//调用 isBadVersion(3) -> false
//调用 isBadVersion(5) -> true
//调用 isBadVersion(4) -> true
//
//所以4 是第一个错误的版本。 
// Related Topics 二分查找
// 👍 312 👎 0
package leetcode.editor.cn;
//278:第一个错误的版本
class FirstBadVersion {
public static void main(String[] args) {
//测试代码
Solution solution = new FirstBadVersion().new Solution();
solution.firstBadVersion(5);
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int start = 1;
int end = n;
int mid;
if (isBadVersion(start)) {
return start;
}
while (start < end) {
mid = start + (end - start) / 2;
if (isBadVersion(mid)) {
end = mid;
} else {
start = mid + 1;
}
}
return start;
}
private boolean isBadVersion(int mid) {
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}