[LeetCode] 278. First Bad Version_Easy tag: Binary Search
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.
Example:
Given n = 5, and version = 4 is the first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
这题思路实际上就跟[LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search很像, 就是找第一个bad version.
04/14/2019 update: 或者说是找 first index isBadVersion()结果为true。
Code
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version): class Solution:
def firstBadVersion(self, n):
l, r = 1, n
while l + 1 < r:
mid = l + (r - l)//2
if isBadVersion(mid):
r = mid
else:
l = mid
if isBadVersion(l):
return l
return r
先判断边界/corner case,然后再判断first index that is true。
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version): class Solution:
def firstBadVersion(self, n):
if isBadVersion(1) : return 1
l, r = 1, n # l must be false
while l + 1 < r:
mid = l + (r - l)//2
if isBadVersion(mid):
r = mid
else:
l = mid
return r
[LeetCode] 278. First Bad Version_Easy tag: Binary Search的更多相关文章
- [LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- [LeetCode] 162. Find Peak Element_Medium tag: Binary Search
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] 704. Binary Search_Easy tag: Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- [LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 35. Search Insert Position_Easy tag: Binary Search
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
Description Given a sorted array of n integers, find the starting and ending position of a given tar ...
随机推荐
- h5 . css入门 2.CSS基础
CSS基础 学习目标 1.CSS简介 2.CSS语法 3.样式的创建 4.两种引入外部样式表的区别 5.样式表的优先级和作用域 6.CSS选择器 7.选择器的权重 8.浮动属性的简单应用 9.HTML ...
- Java面向对象进阶篇(抽象类和接口)
一.抽象类 在某些情况下,父类知道其子类应该包含哪些方法,但是无法确定这些子类如何实现这些方法.这种有方法签名但是没有具体实现细节的方法就是抽象方法.有抽象方法的类只能被定义成抽象类,抽象方法和抽象类 ...
- JQuery EasyUI Layout 在from布局自适应窗口大小
在JQuery EasyUI中,如果直接在form上布局时当窗口大小调整布局不会改变,将布局应用于body时中间隔着一个form,横竖不好弄. 网上有多个解决方案,一般都是写代码,在窗口大小改变时设置 ...
- easyui combobox setValue方法不能触发onSelect事件
//setValue方法不能触发onSelect事件 //$("#FundingSource").combobox("setValue", data.Fundi ...
- Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = integer
Springboot项目,使用postgresql数据库,mybatis做持久层框架, <select id="select" resultMap="BaseRes ...
- 文本分类学习 (九)SVM入门之拉格朗日和KKT条件
上一篇说到SVM需要求出一个最小的||w|| 以得到最大的几何间隔. 求一个最小的||w|| 我们通常使用 来代替||w||,我们去求解 ||w||2 的最小值.然后在这里我们还忽略了一个条件,那就是 ...
- echarts tooltip 自定义提示信息添加圆点
tooltip自定义时,给文字前加圆点 tooltip: { formatter: '{b}<br /><span style="display:inline-block; ...
- stm32中断遵循原则
故障案例: 定时器定时触发一个定时事件,在这个事件里面,会调用一个串口发送程序,发现串口发送数据不完整. 分析: 1.将发送函数剥离,放到独立的线程工作,运行稳定 2.使用单步调试,在定时中断事件中多 ...
- Docker容器与容器云之Docker单机集群部署案例
准备工作: CentOS 7安装docker: #yum -y install docker 1.获取节点所需镜像 --主机执行 #docker pull django #docker pull ha ...
- [Day3]Scanner类、Random类、流程控制语句
1.Scanner类 (1)Scanner类属于引用数据类型 数据类型 变量名=new 数据类型(); (2)每种引用类型都有自己的功能 变量.功能名(); (3)Scanner类是引用数据类型的一种 ...