LintCode-First Bad Version

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

You can determine whether a version is bad by the following interface:

Java:    public VersionControl {        boolean isBadVersion(int version);    }C++:    class VersionControl {    public:        bool isBadVersion(int version);    };Python:    class VersionControl:        def isBadVersion(version)

Find the first bad version.

Note

You should call isBadVersion as few as possible.

Please read the annotation in code area to get the correct way to call isBadVersion in different language. For example, Java is VersionControl.isBadVersion.

Example

Given n=5

Call isBadVersion(3), get false

Call isBadVersion(5), get true

Call isBadVersion(4), get true

return 4 is the first bad version

Challenge

Do not call isBadVersion exceed O(logn) times.

Solution:

 1 /**
 2  * public class VersionControl {
 3  *     public static boolean isBadVersion(int k);
 4  * }
 5  * you can use VersionControl.isBadVersion(k) to judge wether
 6  * the kth code version is bad or not.
 7 */
 8 class Solution {
 9     /**
10      * @param n: An integers.
11      * @return: An integer which is the first bad version.
12      */
13     public int findFirstBadVersion(int n) {
14         if (n==1) return 1;
15
16         int start = 1;
17         int end = n;
18         while (start<=end){
19             int mid = start + (end-start)/2;
20
21             if (!VersionControl.isBadVersion(mid))
22                 start = mid +1;
23             else {
24                 if (mid==1 || !VersionControl.isBadVersion(mid-1))
25                     return mid;
26                 else end = mid-1;
27             }
28         }
29
30         return -1;
31     }
32 }
时间: 2024-08-10 09:31:11

LintCode-First Bad Version的相关文章

Lintcode: First Bad Version 解题报告

First Bad Version http://lintcode.com/en/problem/first-bad-version The code base version is an integer and start from 1 to n. One day, someone commit a bad version in the code case, so it caused itself and the following versions are all failed in the

【Lintcode】074.First Bad Version

题目: 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 isBa

[Lintcode]74. First Bad Version/[Leetcode]278. First Bad Version

[Lintcode]74. First Bad Version [Leetcode]278. First Bad Version 本题难度: [Lintcode]Medium/[Leetcode] Topic: Binary Search Description 我的代码 for Leetcode(在Lintcode中因为isBadVersion调用太多次无法通过) # The isBadVersion API is already defined for you. # @param versi

lintcode 74 First Bad Version

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 isBadVer

maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet

问题: Java compiler level does not match the version of the installed Java project facet 解决方法如下: properties->Java Compiler,修改JDK版本,然后Apply

异常:Unsupported major.minor version 52.0 (Use --stacktrace to see the full trace)

异常:Unsupported major.minor version 52.0 (Use --stacktrace to see the full trace) 正在写一个功能,更新了同事提交的代码之后,出现了如下错误: 1 Fatal error during compilation org.apache.tools.ant.BuildException: java.lang.UnsupportedClassVersionError: org/elasticsearch/index/query

How to Downgrade VMware ESXi 5.5 Virtual Machine Hardware Version to 9 from 10

Shut down the virtual machine in the vSphere client. Remove your virtual machine from the ESXi inventory. Browse your Datastore and find the virtual machine's .vmx file.   Download it to your desktop. Open the .vmx file in Notepad or your favorite te

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param

This version of MySQL doesn&#39;t yet support &#39;LIMIT &amp; IN/ALL/ANY/SOME subquery&#39; 错误解决

原因是内层select语句带有limit子句. 原: update stu_score_childen A  set  A.fScore='-1' where  A.fID in (select B.fID from stu_score_childen B limit 0,50 ) 更改后的 update stu_score_childen A  set  A.fScore='-1' where  A.fID in (select C.fID from (select B.fID from st

[LeetCode] Compare Version Numbers

Question: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The