题意:找最开始坏的版本。
思路:二分查找,注意边界溢出。
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int low = 1;
int high = n;
int mid = low + (high - low) / 2;
while(low != high) {
if(isBadVersion(mid)) {
high = mid;
mid = low + (high - low) /2;
}
else {
low = mid + 1;
mid = low + (high - low) /2;
}
}
return low;
}
};