278. First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version,

all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API

解题思路:

二分查找。需要注意的是,下标是从1开始的。。几次WA都是因为看作0开始了。。我真是大写的蠢。

// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int left = 1;
int right = n;
int mid = left+(right-left)/2;
while (left < right) {
if (isBadVersion(mid) == true) {
right = mid;
} else {
left = mid+1;
}
mid = left+(right-left)/2;
}
return left;
}
};

  

leetcode-9-basic-binary search的更多相关文章

  1. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  2. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  3. Java for LeetCode 095 Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  4. [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  5. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  7. [LeetCode#272] Closest Binary Search Tree Value II

    Problem: Given a non-empty binary search tree and a target value, find k values in the BST that are ...

  8. [LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  9. [LeetCode] questions conclusion_ Binary Search

    Binary Search T(n) = T(n/2) + O(1)   =>    T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...

  10. [LeetCode] 98. Validate Binary Search Tree_Medium

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. 【aspnetcore】抓取远程图片

    找到要抓取的图片地址:http://i.imgur.com/8S7OaEB.jpg 抓取的步骤: 请求图片路径 获取返回的数据 将数据转换为stream 将stream转换为Image 保存Image ...

  2. (转)Linux下java进程CPU占用率高-分析方法

    Linux下java进程CPU占用率高-分析方法 原文:http://itindex.net/detail/47420-linux-java-%E8%BF%9B%E7%A8%8B?utm_source ...

  3. 小G的城堡

    B 小 G 的城堡文件名 输入文件 输出文件 时间限制 空间限制castle.pas/c/cpp castle.in castle.out 1s 128MB题目描述小 G 家有一座城堡.城堡里面有 n ...

  4. 简述null undefined NaN的异同

    1. 类型类型分析: JS中数据类型有5种:string,number,boolean,undefined,object,前四种值类型(基础数据类型),object是引用类型 var a1; //un ...

  5. 【转】monkey实战--测试步骤、常用参数、常规monkey命令

    monkey实战--测试步骤.常用参数.常规monkey命令   简要步骤:adb devices---了解包名--adb shell monkey -p 包名 -v 运行次数(多个参数的组合形成不同 ...

  6. IO(转换流、缓冲流)

    第1章 转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者Output ...

  7. 【整站源码分享】分享一个JFinal3.4开发的整站源码,适合新手学习

    分享这个源码是14年开发上线的<威海创业者>站点的全套整站源码,前后端都在一个包里.当时开发使用的是JFinal1.4,最近改成了JFinal3.4.使用的JSP做的页面.有一定的参考价值 ...

  8. JFinal教程:JFinal极速开发企业实战百集JFinal视频教程发布

    课程名称:JFinal极速开发企业实战 课程长度:100课时 课程作者:小木(909854136) 课程地址:http://edu.csdn.net/course/detail/1968 官网网址:h ...

  9. for循环/计算坐标

    for循环计算坐标 webqq里面有类似桌面的各种图标,是绝对定位的,这样可以拖动改变位置,用浮动的话,没法拖动. <!DOCTYPE html> <html lang=" ...

  10. java代码关闭tomcat程序

    1.通过java代码实现tomcat的关闭 2.tomcatStop.java package test; import java.io.BufferedReader; import java.io. ...