您的位置:首页 > 其它

Lintcode 第一个错误版本

2017-08-04 10:51 260 查看
/**
* class SVNRepo {
*     public:
*     static bool isBadVersion(int k);
* }
* you can use SVNRepo::isBadVersion(k) to judge whether
* the kth code version is bad or not.
*/
class Solution {
public:
/**
* @param n: An integers.
* @return: An integer which is the first bad version.
*/
int findFirstBadVersion(int n) {
// write your code here
if(n <= 0) {
return 0;
}

int low = 1, high = n;
while(low <= high) {
int mid = low + (high - low) / 2;
if(SVNRepo::isBadVersion(mid)) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return low;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: