题目

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.

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

分析

题目要求在产品的n个版本中找到第一个bad version。在所有版本中,若是当前i版本是bad,则后续i+1版本也是bad。

考察二分查找的应用。

AC代码

// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
if (n < 1)
return -1;
int lhs = 1, rhs = n; while (lhs < rhs)
{
int mid = lhs + (rhs - lhs) / 2;
//找到bad version
if (isBadVersion(mid))
{
rhs = mid;
}
//mid并不是bad version 则first bad肯定在右边
else{
lhs = mid + 1;
}
}//while
return lhs;
}
};

LeetCode(278)First Bad Version的更多相关文章

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  4. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  5. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  6. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  7. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. Jmeter4.0----编写测试脚本(5)

    1.说明 以HTTP请求为例,和小伙伴门分享一下jmeter测试脚本的基本编写步骤 2.步骤说明 第一步:打开jmeter,更改测试计划名称为 Test batchSignForDir(修改计划名称, ...

  2. 日志AOP的实现

    /// <summary> /// 日志AOP拦截 /// </summary> [AttributeUsage(AttributeTargets.Method, AllowM ...

  3. CF1088D Ehab and another another xor problem

    思路: 根据异或的性质一位一位来搞.参考了https://blog.lucien.ink/archives/362/ 实现: #include <bits/stdc++.h> using ...

  4. java项目定时任务实现

    首先配置spring-context.xml文件 在xmlns 下加如下代码 xmlns:task="http://www.springframework.org/schema/task&q ...

  5. 键盘各键对应的ASCII码值(包括鼠标和键盘所有的键)

    ESC键 VK_ESCAPE (27)回车键: VK_RETURN (13)TAB键: VK_TAB (9)Caps Lock键: VK_CAPITAL (20)Shift键: VK_SHIFT ($ ...

  6. Java中方法的继承以及父类未被子类覆盖的方法调用的问题

    在看java继承这一块的时候发现了一个问题,即父类未被子类覆盖的方法是如何调用的? 是子类拥有了父类的该方法只是没有显示表示,还是子类调用了父类的该方法. 为此做了一下验证 代码如下: public ...

  7. Python + selenium之unitest(1)

    单元测试负责对最小的软件设计单元(模块)进行验证,它使用软件设计文档中对模块的描述作为指南,对重要的程序进行测试以发现模块中的错误. 下面演示不用测试框架的单元测试: # 计算器类 class Cou ...

  8. Windows 7, Visual Studio 2015下编译Webkit

    因工作需要,需要编译Windows版本的Webkit,中间走了不少弯路,都记录下来,供大家参考!也随时欢迎大家讨论(QQ群:345802342) 整个编译工作参考的是官方文档:https://webk ...

  9. 洛谷 P1001 A+B Problem

    题目描述 输入两个整数a,b,输出它们的和(|a|,|b|<=10^9). 注意 1.pascal使用integer会爆掉哦! 2.有负数哦! 3.c/c++的main函数必须是int类型,而且 ...

  10. [numpy] 基础练习 (一)

    Numpy常用总结 基础要打牢,恩. 基础 # 0 - 9 arr = np.arange(10) # 3*3 bool np.full((3,3),true,dtype = bool) np.one ...