思路:二分查找。要注意int mid = (start - end) / 2 + end;
或者 int mid = (end - start) / 2 + start;
代码:
/* 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 = 1;
int end = n;
while (start < end) {
int mid = (end - start) / 2 + start;
if (!isBadVersion (mid)) {
start= mid + 1;
}
else {
end = mid;
}
}
return start;
}
}