[Java]LeetCode278. 第一個錯誤的版本 | First Bad Version

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-oodrqpwc-ky.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git

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.github

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 的次數。spa

示例:code

給定 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 }

11mshtm

 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 }
相關文章
相關標籤/搜索