278. 第一个错误的版本
思路:
相当于转化成查找第一个true,直接二分搜索即可。
简单题我重拳出击
代码:
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
long long left = 0,right = n;
while(left<right){
long long mid = (left+right)/2;
bool res = isBadVersion(mid);
if(res){
right = mid;
}
else if(!res){
left = mid+1;
}
}
return left;
}
};