The code base version is a integer start from 1 to N. One day, someone committed a bad version in the code case, so it caused this version and the following versions is all FA iled in the unit tests. Find the first bad version.
Determine which version is the first bad one isBadVersion
. The details interface can be found in the code ' s annotation part.
Notice
Please read the annotation in code area to get the correct-on-the-isbadversion in different language. For example, Java isSVNRepo.isBadVersion(v)
Example
Given n = 5
:
isBadVersion(3) -> falseisBadVersion(5) -> trueisBadVersion(4) -> true
Here we were 100% sure that the 4th version was the first bad version.
Analysis:
Two-point lookup.
1 /**2 * public class Svnrepo {3 * public static Boolean isbadversion (int k);4 * }5 * can use Svnrepo.isbadversion (k) to judge whether6 * The KTH code version is bad or not.7 */8 classSolution {9 /**Ten * @param n:an integers. One * @return: An integer which are the first bad version. A */ - Public intFindfirstbadversion (intN) { - intStart =1; the intEnd =N; - - while(Start <=end) { - intMid = (start + end)/2; + if(Svnrepo.isbadversion (mid) = =false) { -Start = mid +1; +}Else { AEnd = Mid-1; at } - } - - returnstart; - } -}
First Bad Version