74.first bad version

/**
 * public class SVNRepo {
 *     public static boolean isBadVersion(int k);
 * }
 * you can use SVNRepo.isBadVersion(k) to judge whether 
 * the kth code version is bad or not.
*/
class Solution {
/**
 * @param n: An integers.
 * @return: An integer which is the first bad version.
 */
public int findFirstBadVersion(int n) {
    // write your code here
    int start = 1;
    int end = n;
    while (start + 1 < end) {
        int mid = start + (end - start) / 2;
        if (SVNRepo.isBadVersion(mid)) end = mid;
        else start = mid;
    }
    if (SVNRepo.isBadVersion(start)) return start;
    else return end;
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容