278:第一个错误的版本
This commit is contained in:
parent
987c6715a7
commit
6a398dd000
60
src/main/java/leetcode/editor/cn/FirstBadVersion.java
Normal file
60
src/main/java/leetcode/editor/cn/FirstBadVersion.java
Normal file
@ -0,0 +1,60 @@
|
||||
//你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有
|
||||
//版本都是错的。
|
||||
//
|
||||
// 假设你有 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)
|
||||
|
||||
}
|
16
src/main/java/leetcode/editor/cn/FirstBadVersion.md
Normal file
16
src/main/java/leetcode/editor/cn/FirstBadVersion.md
Normal file
@ -0,0 +1,16 @@
|
||||
<p>你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。</p>
|
||||
|
||||
<p>假设你有 <code>n</code> 个版本 <code>[1, 2, ..., n]</code>,你想找出导致之后所有版本出错的第一个错误的版本。</p>
|
||||
|
||||
<p>你可以通过调用 <code>bool isBadVersion(version)</code> 接口来判断版本号 <code>version</code> 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。</p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>给定 n = 5,并且 version = 4 是第一个错误的版本。
|
||||
|
||||
<code>调用 isBadVersion(3) -> false
|
||||
调用 isBadVersion(5) -> true
|
||||
调用 isBadVersion(4) -> true
|
||||
|
||||
所以,4 是第一个错误的版本。 </code></pre>
|
||||
<div><div>Related Topics</div><div><li>二分查找</li></div></div>\n<div><li>👍 312</li><li>👎 0</li></div>
|
4
src/main/java/leetcode/editor/cn/VersionControl.java
Normal file
4
src/main/java/leetcode/editor/cn/VersionControl.java
Normal file
@ -0,0 +1,4 @@
|
||||
package leetcode.editor.cn;
|
||||
|
||||
public class VersionControl {
|
||||
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user