278. First Bad Version
一句话思路:找数字变成了找所谓的bad version,换了个名字而已。挺没有思考含量的一道题,其实。随便看看就行吧。
一刷报错:
- 这题第一位是1,可能是因为版本1是1吧
- 不用SVNRepo.isBadVersion(end),直接isBadVersion(end)就行了
/* 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 start;
int end;
int mid;
//binary search
start = 1;
end = n;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (isBadVersion(mid)) {
end = mid;
}
else start = mid;
}
if(isBadVersion(start)) {
return start;
}
else if(isBadVersion(end)) {
return end;
}
return -1;
}
}