Created
May 28, 2018 14:50
-
-
Save yitonghe00/7953e7a02057d23ecf74a02bd4232c6c to your computer and use it in GitHub Desktop.
278. First Bad Version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The isBadVersion API is defined in the parent class VersionControl. | |
boolean isBadVersion(int version); */ | |
public class Solution extends VersionControl { | |
public int firstBadVersion(int n) { | |
int begin = 0, end = n, mid; | |
while(begin < end) { | |
mid = begin + (end - begin) / 2; | |
if(isBadVersion(mid)) { | |
end = mid; //Ensure there is at least one bad version in the range. Also end will move at least one step. | |
} else { | |
begin = mid + 1; //begin will move at least one step. | |
} | |
} | |
//start == end == mid | |
return start; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment