Lintcode 74 First Bad Version solution 题解

【题目描述】

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 callisBadVersionto help you determine which version is the first bad one. The details interface can be found in the code's annotation part.

代码库的版本号是从1 到n的整数。某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错。请找出第一个错误的版本号。

你可以通过isBadVersion的接口来判断版本号 version 是否在单元测试中出错,具体接口详情和调用方法请见代码的注释部分。

注意:请阅读上述代码,对于不同的语言获取正确的调用isBadVersion的方法,比如java的调用方式是SVNRepo.isBadVersion(v)

【题目链接】

www.lintcode.com/en/problem/first-bad-version/

【题目解析】

从最先出错的版本所在的位置开始,其后的所有版本也都是错误的,所以此题使用二分法解决。二分搜索的范围,使用[0,n)来控制边界。需要查找出错版本的左边界,找到时,令end=mid,未找到则令start=mid。最后返回的start就是第一个出错的版本号。

【参考答案】

www.jiuzhang.com/solutions/first-bad-version/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容