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.
Given n = 5
:
isBadVersion(3) -> falseisBadVersion(5) -> trueisBadVersion(4) -> true
Here we were 100% sure that the 4th version was the first bad version.
/*** public class Svnrepo {* public static Boolean isbadversion (int k); *} * can use Svnrepo.isbadversion (k) t O Judge whether * the KTH code version is bad or not.*/classSolution {/** * @paramn:an integers. * @return: An integer which are the first bad version. */ Public intFindfirstbadversion (intN) {//Write your code here if(N <= 1) return1; intleft = 1; intright =N; while(Left <Right ) { intMid = left + (right-left)/2; if(Svnrepo.isbadversion (mid)) right=mid; Else Left= Mid + 1; } returnLeft ; }}
Lintcode-medium-first Bad Version