problem
solution1:遍历;
// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int ver = ;
while(ver<n)
{
if(isBadVersion(ver)) return ver;
ver++;
}
return ver;
}
};
solution2:二分法;
// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int left = , right = n, mid;
while(left<right)
{
mid = left + (right-left)/;
if(isBadVersion(mid)) right = mid;
else left = mid + ;//err.
}
return left;
}
};
参考
1. Leetcode_278_First Bad Version;
完