LeetCode(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.
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的更多相关文章
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- JAVA 框架之面向对象设计原则
面向对象设计原则: 单一职责原则 SRP : 一个类或者行为只做一件事 . 降低代码冗余,提高可重用性,可维护性,可扩展性,可读性 使用组合形式 里氏替换原则 LSP : 所有引用基类 ...
- 洛谷P2062 分队问题(dp)
题意 题目链接 给定n个选手,将他们分成若干只队伍.其中第i个选手要求自己所属的队伍的人数大等于a[i]人. 在满足所有选手的要求的前提下,最大化队伍的总数. 注:每个选手属于且仅属于一支队伍. So ...
- 移动端rem单位和px单位换算
rem单位是根据html元素的单位在页面根据不同的手机屏幕分辨率动态整体的按比例缩小或放大字体. 假如html{font-size: 14px;},那么1rem=14px; 一个div宽度48px,那 ...
- HBase数据模型(1)
HBase数据模型(1) HBase数据模型(2) 1.0 HBase的特性 Table HBase以表(Table)的方式组织数据,数据存储在表中. Row/Column 行(Row)和列(Colu ...
- layout_weight属性
layout_weight 某个控件text多行,第一行对齐,baselineAligned = "false" 可使控件对齐 weight 计算规则 剩余尺寸=父布局尺寸-子布局 ...
- 使用PowerShell 获取azure image publisher offer sku 信息
使用azure powershell 获取指定区域的可用镜像 publisher offer sku信息 param ( [parameter(Mandatory = $false)] $Locati ...
- JavaScript_8_比较,条件语句
1. 比较运算符 ==(值相等,类型可以不等), ===(值和类型必须都相等), !=, >, <, >=, <= 2. 逻辑运算符 &&.||.! 3. 条件 ...
- HTML之基本语法(表单)
一.表单的基本介绍 表单:就是互联网上用于收集用户信息的一种结构,在HTML当中事先定义好了一种标签来完成此事,标签名称为form,它是一个双标签<form action="" ...
- js中异步方案比较完整版(callback,promise,generator,async)
JS 异步已经告一段落了,这里来一波小总结 1. 回调函数(callback) setTimeout(() => { // callback 函数体 }, 1000) 缺点:回调地狱,不能用 t ...
- C++中malloc / free 和 new / delete 的区别?
1.malloc/free 是C++/C语言的标准库函数,New/delete是C++运算符:都是用于申请动态内存和释放内存. 2.new做两件事:分配内存和调用类的构造函数,delete是:调用类的 ...