74. 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.

解法一:

 /**
* class SVNRepo {
* public:
* static bool isBadVersion(int k);
* }
* you can use SVNRepo::isBadVersion(k) to judge whether
* the kth code version is bad or not.
*/
class Solution {
public:
/*
* @param n: An integer
* @return: An integer which is the first bad version.
*/
int findFirstBadVersion(int n) {
int start = ;
int end = n; while (start + < end) {
int mid = start + (end - start) / ; if (SVNRepo::isBadVersion(mid) == false) {
start = mid;
}
else {
end = mid;
}
} return (SVNRepo::isBadVersion(start) ? start : end);
}
};

74. First Bad Version 【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  5. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  6. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  7. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  8. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

随机推荐

  1. [BZOJ 2752] 高速公路

    Link: BZOJ 2752 传送门 Solution: 虽然有期望,但实际上就是除了个总数…… 此题计算总代价明显还是要使用对每个$w_i$计算贡献的方式: $w_i的贡献为w_i*(i-l+1) ...

  2. [Codeforces 7E] Defining Macros

    Link:http://codeforces.com/problemset/problem/7/E Brief Introduction:一个表达式由多个“Macros”组成,每个Macro都为一个整 ...

  3. AngularJS的加载执行过程

    1. HTML页面的加载,这会触发加载页面包含的所有JS (包括 AngularJS) 2. AngularJS启动,搜寻所有的指令(directive) 3. 找到ng-app,搜寻其指定的模块(M ...

  4. python 爬取36K新闻

    代码如下: from urllib import request url = 'http://36kr.com/api/info-flow/newsflash_columns/newsflashes? ...

  5. Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface解决方案

    原文:http://pimin.net/archives/432 环境:Eclipse LUNA.Spring 4.1.1.或Spring 4.3.3.Spring Data Elasticsearc ...

  6. cs-HtmlHelpers

    ylbtech-Unitity: cs-HtmlHelpers PagingHelpers.cs  SelectInputHelpers.cs TreeHelpers.cs 1.A,效果图返回顶部   ...

  7. DICOM中几个判断图像方向的tag

    在DICOM标准里,有三个TAG与成像的方向相关. 参考来源:Kitware关于DICOM方向的说明 http://public.kitware.com/IGSTKWIKI/index.php/DIC ...

  8. Linux(CentOS)修改IP地址

    登陆连接centos系统,输入 ifconfig 可以查看到当前本机的IP地址信息 一 临时修改IP地址: 1.假如查询IP为1.118,输入 ifconfig eth0 (默认是第一个网卡) 后面接 ...

  9. [TypeScript] Infer the Return Type of a Generic Function Type Parameter

    When working with conditionals types, within the “extends” expression, we can use the “infer” keywor ...

  10. Android基础新手教程——4.1.2 Activity初窥门径

    Android基础新手教程--4.1.2 Activity初窥门径 标签(空格分隔): Android基础新手教程 本节引言: 上一节中我们对Activity一些主要的概念进行了了解,什么是Activ ...