Lettocde_278_First Bad Version
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 haven
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 APIbool isBadVersion(version)
Which will return whetherversion
Is bad. Implement a function to find the first bad version. You shoshould minimize the number of cballs to the API.
Ideas:
(1) If a series of version numbers are specified, you must find the version number of the first error. (The current version is based on the previous version. If an error occurs in a specific version, subsequent versions of the current version will fail)
(2) This question mainly describes binary search. You can set 1, 2 ,... n series of versions are represented as good, good ,......, good, bad, bad ,....., you only need to use the Binary Search Algorithm for determination. Note that if you find that the current version is good, you need to point start to the next position of the mid.
(3) For details, see the code below. I hope this article will help you.
Package leetcode; public class First_Bad_Version {// find the first bad public int firstBadVersion (int n) {int start = 0; int end = n; while (start <= end) {int mid = start + (end-start)> 1; // if it is bad, find if (isBadVersion (mid) {end = mid ;} else {start = mid + 1 ;}} return end;} static boolean isBadVersion (int version) {return true ;}}