first_bad_version
first_bad_version.cpp 源码
// The API isBadVersion is defined for you.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int low = 1, high = n;
while (low <= high) {
int mid = low + (high - low) / 2;
if (isBadVersion(mid)) {
if (mid == low || !isBadVersion(mid-1)) {
return mid;
}
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
};
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦