lintcode74- First Bad Version- medium

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.

Notice

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

Example

Given n = 5:

isBadVersion(3) -> false
isBadVersion(5) -> true
isBadVersion(4) -> true

Here we are 100% sure that the 4th version is the first bad version.

Challenge

You should call isBadVersion as few as possible.

OOXX二分法(find the first X)。特征:布尔值哪一种。O:false;X:true。

/**
 * 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.
*/

public class Solution {
    /*
     * @param n: An integer
     * @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;
        }
        return end;
    }
}
时间: 2024-07-31 17:51:23

lintcode74- First Bad Version- medium的相关文章

Developing a plugin framework in ASP.NET MVC with medium trust

http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx January 7, 2011 10:06 Tweet I’ve recently spent quite a lot of time researching and prototyping different ways to create a plugin engine in ASP.NET MVC3 and prim

Manifest merger failed : uses-sdk:minSdkVersion 9 cannot be smaller than version 10 declared in library

Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed : uses-sdk:minSdkVersion 9 cannot be smaller than version 10 declared in library [org.piwik.sdk:piwik-sdk:0.0.3] /Users/weichunsheng/Documents/android_app/app/build

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,

【DVWA】【SQL Injection】SQL注入 Low Medium High Impossible

1.初级篇 low.php 先看源码,取得的参数直接放到sql语句中执行 if( isset( $_REQUEST[ 'Submit' ] ) ) { // Get input $id = $_REQUEST[ 'id' ]; // Check database $query = "SELECT first_name, last_name FROM users WHERE user_id = '$id';"; http://localhost/DVWA-master/vulnerabi

[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

【LEETCODE】68、分治递归,medium级别,题目:95、120、91

package y2019.Algorithm.dynamicprogramming.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.dynamicprogramming.medium * @ClassName: NumDecodings * @Author: xiaof * @Description: 91. Decode Ways * A message containing letters from

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

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