博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lintcode-medium-First Bad Version
阅读量:5266 次
发布时间:2019-06-14

本文共 1311 字,大约阅读时间需要 4 分钟。

The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version.

You can call isBadVersion to help you determine which version is the first bad one. The details interface can be found in the code's annotation part.

 

Given n = 5:

isBadVersion(3) -> falseisBadVersion(5) -> trueisBadVersion(4) -> true

Here we are 100% sure that the 4th version is the 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                if(n <= 1)            return 1;                int left = 1;        int right = n;                while(left < right){            int mid = left + (right - left) / 2;                        if(SVNRepo.isBadVersion(mid))                right = mid;            else                left = mid + 1;        }                return left;    }}

 

转载于:https://www.cnblogs.com/goblinengineer/p/5300479.html

你可能感兴趣的文章
机房收费系统——报表
查看>>
How to unshelve many shelves at same time
查看>>
table中checkbox选择多行
查看>>
动态链接库
查看>>
Magento开发文档(三):Magento控制器
查看>>
使用Docker官方的Django包【转】
查看>>
SuperSocket 学习
查看>>
给培训学校讲解ORM框架的课件
查看>>
此实现不是 Windows 平台 FIPS 验证的加密算法的一部分
查看>>
性能调优攻略
查看>>
线段树模板讲解
查看>>
ie6解决png图片透明问题
查看>>
瞬间的永恒
查看>>
H3C交换机DHCP&nbsp;Server配置的六个方面
查看>>
docker overlay网络实现
查看>>
mysql方言不支持blob类型解决方案
查看>>
2019-8-5 考试总结
查看>>
jquery javascript 回到顶部功能
查看>>
JS中实现字符串和数组的相互转化
查看>>
win10 进入安全模式的方法
查看>>