Leetcode 278 First Bad Version
题意:提供一个isBadVersion()
API, 产品在某个地方位置就出现了bad version,求第一次出现bad version的位置
https://leetcode.com/problems/first-bad-version/
分析:这是一道二分,而且是XXXXXOOOO (O代表是bad version,这道题是找到第一个O)
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int l = 1;
int r = n;
while (l < r) {
int mid = l + (r-l) / 2;
if(isBadVersion(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
};