278. First Bad Version

二分法,如果isBadVersion返回true则坏版本在左边,right = mid,否则 left = mid + 1。

注意溢出问题 left+(right - left) /2

最后return left right均可

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 1, right = n;
while(left < right){
int mid = left + (right - left) / 2;
if(isBadVersion(mid)) right = mid;
else left = mid + 1;
}
return left;
}
}

11/3 <binary search>的更多相关文章

  1. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  2. [UCSD白板题] Binary Search

    Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...

  3. 关于binary search的一点解惑

    在写binary search时对于mid的计算我最开始使用的是 mid = (low + high)/2; 后来看到在很多的实现为 mid = low + (high - low)/2; 想了一下两 ...

  4. 【Unique Binary Search Trees II】cpp

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  5. Binary Search

    Binary Search                              [原文见:http://www.topcoder.com/tc?module=Static&d1=tuto ...

  6. 1099. Build A Binary Search Tree (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  7. LintCode-Implement Iterator of Binary Search Tree

    Design an iterator over a binary search tree with the following properties: Elements are visited in ...

  8. Binary Search Tree In-Order Traversal Iterative Solution

    Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...

  9. 九章算法系列(#2 Binary Search)-课堂笔记

    前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一 ...

随机推荐

  1. C++中enum(转载)

    原文地址:http://www.cnblogs.com/ForFreeDom/archive/2012/03/22/2412055.html 1.为什么要用enum       写程序时,我们常常需要 ...

  2. golang基础之第一个go程序

    编写 Hello World 创建文件 hello.go,不写入任何内容.按照如下的命令尝试进行编译 $ go run hello.go 将会打印出如下错误: package main: hello. ...

  3. 【shell脚本】定时备份日志===logBackup.sh

    定时备份日志 设置执行权限 [root@VM_0_10_centos shellScript]# chmod a+x logBackup,sh 脚本内容 [root@VM_0_10_centos sh ...

  4. 【shell脚本】通过位置变量创建Linux账户及密码===addUser.sh

    通过位置变量创建Linux账户及密码 脚本内容 [root@VM_0_10_centos shellScript]# vi addUser.sh #!/bin/bash # 通过位置变量创建系统账户及 ...

  5. Flask restful源码分析

    Flask restful的代码量不大,功能比较简单 参见 http://note.youdao.com/noteshare?id=4ef343068763a56a10a2ada59a019484

  6. IDA分析时添加新的C语言结构体

    View - Open Subviews - Local Type - INSERT键 - 输入新结构体 - 右击"Synchornize to idb" 之后再分析处按 T 就可 ...

  7. Kafka学习笔记之Kafka High Availability(上)

    0x00 摘要 Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦一个或多个Broker宕机,则宕机期间其上所有Partition都无法继续提供服务.若该Broker永 ...

  8. 刨树根,抓住redis 进行七连问

    追着 redis 进行七连问 Hello Redis 有几个问题想请教你 Hello,Redis! 我们相处已经很多年了,从模糊的认识到现在我们已经深入结合,你的好我一直都知道也一直都记住,能否在让我 ...

  9. python 字符前缀,运算符、换行符、数据类型和变量

    补充 *)/ 表示的除法即使是整数,结果也是浮点数 *)python表示的整数是没有大小限制的.而某些语言根据其储存长度是有大小限制的.例如Java对32位整数的范围限制在-2147483648-21 ...

  10. 一句DELETE引发的加班(Mysql 恢复Delete删除的数据)

    本机用的Navicat连mysql测试DB又连了正式DB,因为本地与正式要频繁操作所以都打开了很多查询,本来要DELETE删除测试DB的数据,没看清在正式环境执行了.共删除了325条数据,然后在网上找 ...