Lintcode 74 First Bad Version solution 题解

来源:互联网 发布:淘宝买家秀模板 编辑:程序博客网 时间:2024/06/05 12:44

【题目描述】

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/

原创粉丝点击