http://www.lintcode.com/en/problem/first-bad-version/
从某个版本之后都是错误版本,找出第一个错误版本
版本号可能为Integer.MAX_VALUE,会越界!用模板
class Solution {
/**
* @param n: An integers.
* @return: An integer which is the first bad version.
*/
public int findFirstBadVersion(int n) {
// write your code here
int beg = 1;
int end = n;
while (beg + 1 < end) {
int mid = beg + (end - beg) / 2;
if (!SVNRepo.isBadVersion(mid)) {
beg = mid;
} else {
end = mid;
}
}
if (SVNRepo.isBadVersion(beg)) {
return beg;
} else {
return end;
}
}
}