题意:找到第一个出问题的版本

二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出

 // Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int l = , r = n , ans ;
while(l <= r){
int mid = l + (r - l + ) / ;
if(!isBadVersion(mid)){
l = mid + ;
}
else {
r = mid - ;
ans = mid;
}
}
return ans;
}
};

Leetcode 278 First Bad Version 二分查找(二分下标)的更多相关文章

  1. (medium)LeetCode 278.First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  2. Java [Leetcode 278]First Bad Version

    题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...

  3. [LeetCode] 278. First Bad Version 第一个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  4. LeetCode 278.First Bad Version(E)(P)

    题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...

  5. leetcode 278. First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  6. [leetcode]278. First Bad Version首个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  7. 【leetcode边做边学】二分查找应用

    很多其它请关注我的HEXO博客:http://jasonding1354.github.io/ 简书主页:http://www.jianshu.com/users/2bd9b48f6ea8/lates ...

  8. 零基础学习java------day12------数组高级(选择排序,冒泡排序,二分查找),API(Arrays工具类,包装类,BigInteger等数据类型,Math包)

    0.数组高级 (1)选择排序 它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的起始位置 ...

  9. Java基础知识强化60:经典查找之二分查找

    1. 二分查找       二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 比较 ...

随机推荐

  1. java内存知识

    java对内存的分类. (网上资料)程序中用来存放数据的内存分为四块,另有一块用于存放代码 1.堆:存放所有new出来的对象(我们知道java并没有全局变量这个概念,有人是把它单独放在properti ...

  2. js资源

    http://www.oschina.net/p/ace-editor https://git.oschina.net/pandao/editor.md/blob/master/editormd.js ...

  3. quick-cocos2d-x之testlua之mainMenu.lua

    require "helper" require "testResource" require "ActionsTest.ActionsTest&qu ...

  4. 主DNS配置

    一,安装BIND [root@localhost ~]# yum install bind bind-chroot bind-utils Loaded plugins: product-id, sub ...

  5. who is the middle

    Description FJ is surveying his herd to find the most average cow. He wants to know how much milk th ...

  6. Oracle DB 存储增强

    • 设置Automatic Storage Management (ASM)  快速镜像 再同步 • 使用ASM 首选镜像读取 • 了解可伸缩性和性能增强 • 设置ASM 磁盘组属性 • 使用SYSA ...

  7. apache的80端口分发

    打开 conf 文件夹,找到下面的 httpd.conf 更改Listen 后面的端口号为:80:默认就是80端口 去掉下面的相关注释: #LoadModule proxy_module module ...

  8. noip2010-t2

    题目大意:小明过生日的时候,爸爸送给他一副乌龟棋当作礼物.乌龟棋的棋盘是一行 N个格子,每个格子上一个分数(非负整数).棋盘第 1 格是唯一 的起点,第 N格是终点,游戏要求玩家控制一个乌龟棋子从起点 ...

  9. java8的接口新特性(可以有方法体的接口)(转)

    以前Java的接口中定义的方法不可以有方法体,这样试用起来,有时候听不方便的,当有多个类实现了想同的接口,接口中某一些方法的实现体可能都是一样的时候,这样无疑浪费了很多时间,在写重复的代码(或者说co ...

  10. C++字符转码

    wchar_t* U8ToUnicode(char* szU8) { //UTF8 to Unicode //由于中文直接复制过来会成乱码,编译器有时会报错,故采用16进制形式 //char* szU ...