[Java]LeetCode278. 第一个错误的版本 | First Bad Version

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9756798.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example:

Given n = 5, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true

Then 4 is the first bad version. 


你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。

假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。

你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。

示例:

给定 n = 5,并且 version = 4 是第一个错误的版本。

调用 isBadVersion(3) -> false
调用 isBadVersion(5) -> true
调用 isBadVersion(4) -> true

所以,4 是第一个错误的版本。 

10ms
 1 /* The isBadVersion API is defined in the parent class VersionControl.
 2       boolean isBadVersion(int version); */
 3 
 4 public class Solution extends VersionControl {
 5     public int firstBadVersion(int n) {
 6         
 7     int start = 1;
 8     int end = n;
 9     while (start < end) {
10         int mid = start + (end - start) / 2;
11         if (isBadVersion(mid)) {
12             end = mid;
13         } else {
14             start = mid + 1;
15         }
16     }
17     return start;        
18     }
19 }

11ms

 1 public class Solution extends VersionControl {
 2     public int firstBadVersion(int n) {
 3         // Left included / Right not included will overflow when:
 4         // n = 2^31 - 1.
 5         // Thus, use left included / Right included instead.
 6         int l = 1;
 7         int r = n;
 8 
 9         while (l <= r) {
10             int m = l + (r - l) / 2;
11 
12             // Upper bound binary search:
13             //  Try to find first index: m, which:
14             //  versions[m] -> versions[n-1] are all bad versions.
15             //  i.e. m is the first index of bad versions.
16             if (isBadVersion(m)) {
17                 // Search left part.
18                 r = m - 1;
19             } else {
20                 // Search right part.
21                 l = m + 1;
22             }
23         }
24 
25         // 'l' is the first index,
26         //  which versions[l] -> versions[n-1] are all bad versions,
27         return l;
28     }
29 }

12ms

 1 public class Solution extends VersionControl {
 2     public int firstBadVersion(int n) {
 3         int start = n;
 4         int end = n;
 5         while (start > 1 && isBadVersion(start)) {
 6             end = start;
 7             start = start / 2;
 8         }
 9         
10         while (start + 1 < end) {
11             int mid = start + (end - start) / 2;
12             if (!isBadVersion(mid)) {
13                 start = mid;
14             } else {
15                 end = mid;
16             }
17         }
18         if (isBadVersion(start)) {
19             return start;
20         }
21         if (isBadVersion(end)) {
22             return end;
23         }
24         return -1;
25     }
26 }

13ms

 1 public class Solution extends VersionControl {
 2     public int firstBadVersion(int n) {
 3         if (n == 1) {
 4             return n;
 5         }
 6         int start = 1;
 7         int end = n;
 8         while (true) {
 9             int mid = start + (end - start) / 2;
10             if (isBadVersion(mid)) {
11                 if (mid == 0 || !isBadVersion(mid - 1)) {
12                     return mid;
13                 }
14                 end = mid - 1;
15             } else {
16                 start = mid + 1;
17             }
18         }
19     }
20 }

14ms

 1 public class Solution extends VersionControl {
 2     public int firstBadVersion(int n) {
 3         int beginning = 0;
 4         int end = n; 
 5         while (beginning < end) {
 6             
 7             int middle = beginning + (end - beginning) / 2; 
 8             System.out.println(beginning + " " + end);
 9 
10             boolean isBad = isBadVersion(middle); 
11             
12             if (isBad) {
13                 end = middle;
14             } else {
15                 beginning = middle + 1; 
16             }
17         }
18         return beginning; 
19         
20     }
21 }
 
原文地址:https://www.cnblogs.com/strengthen/p/9756798.html