/**
* public class VersionControl {
* public static boolean isBadVersion(int k);
* }
* you can use VersionControl.isBadVersion(k) to judge wether
* the kth code version is bad or not.
*/
class Solution {
/**
* @param n: An integers.
* @return: An integer which is the first bad version.
*/
public int findFirstBadVersion(int n) {
// 2015-4-11
if (n <= 0) {
return 0;
}
int start = 1;
int end = n;
while (start + 1 < end) {
int mid = (start + end) / 2;
if (VersionControl.isBadVersion(mid)) {
end = mid;
} else {
start = mid;
}
}
// 有可能第一个version就是BadVersiuon
if (VersionControl.isBadVersion(start)) {
return start;
}
return end;
// 不考虑没有BadVersion的情况
}
}
[LintCode]First Bad Version/** * public class VersionControl { * public static boolean isBadVersion(int k); * } * you can use VersionControl.isBadVersion(k) to judge wether * the kth cod