调用题目给的API,二分查找第一个错误的版本。
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
long long left = 1, right = n;
while(left < right) {
int mid = (left + right) / 2;
if(isBadVersion(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return right;
}
};