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

you can use SVNRepo::isBadVersion(k) to judge whether the kth code version is bad or not.

题解:

  注意:SVNRepo::isBadVersion(k)不能写成SVNRepo.isBadVersion(k), Lintcode要求这么写,后者编译错误。

class Solution {
public:
    /**
     * @param n: An integers.
     * @return: An integer which is the first bad version.
     */
    int findFirstBadVersion(int n) {
        int start = 1, 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-12-09 08:58:39

【Lintcode】074.First Bad Version的相关文章

【问题解决】Project facet Java version 1.7 is not supported.

在移植eclipse项目时,如果遇到 “Project facet Java version 1.7 is not supported.” 项目中的jdk1.7不支持.说明项目是其他版本jdk编译的,在eclipse里运行时会报版本不支持. 解决办法: 法1,选中项目 Properties , 选择 Project Facets,右击选择 Java , Change Version ...          即可. 法2,在 项目的目录下有一个.settings的文件夹,该文件夹下有一个org.

【Lintcode】Median of two Sorted Arrays

class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: a double whose format is *.5 or *.0 */ int check(vector<int>& A, vector<int>& B,int k,int m) { int M = A.size(); int N = B.size(); int

【转】解决Cannot change version of project facet Dynamic web module to 2.5

http://blog.csdn.net/steveguoshao/article/details/38414145 我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一般现在至少都是2.5,在Project Facets里面修改Dynamic web module为2.5的时候就会出现Cannot change version of project

【问题解决】Project facet Java version 1.7 is not suppor

在移植eclipse项目时,如果遇到 "Project facet Java version 1.7 is not supported." 项目中的jdk1.7不支持.说明项目是其他版本jdk编译的,在eclipse里运行时会报版本不支持. 解决办法: 法1,选中项目 Properties , 选择 Project Facets,右击选择 Java , Change Version 即可 法2,在 项目的目录下有一个.settings的文件夹,该文件夹下有一个org.eclipse.w

【问题解决】Project facet Java version 1.7 (或者1.8)is not supported.

在移植eclipse项目时,如果遇到 “Project facet Java version 1.7 is not supported.” 项目中的jdk1.7不支持.说明项目是其他版本jdk编译的,在eclipse里运行时会报版本不支持. 解决办法: 法1,选中项目 Properties , 选择 Project Facets,右击选择 Java , Change Version ...          即可. 法2,在 项目的目录下有一个.settings的文件夹,该文件夹下有一个org.

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

【Lintcode】153.Combination Sum II

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Example Given candidate set [10,1,6,7,2,1,5] 

【Lintcode】137.Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. How we serialize an undirected graph: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each

【Lintcode】069.Binary Tree Level Order Traversal

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). Example Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 题解: 三种