[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 ...
随机推荐
- tsm 切记
切记不可删除节点,如果删除下面带的数据也会删除
- 单目三维稠密重建方案:Quadtree-accelerated Real-time Monocular Dense Mapping
论文:This is a monocular dense mapping system following the IEEE Robotics and Automation Letters (RA-L ...
- PPT文件分析摘记
将.pptx文件后缀改成.rar,然后解压 ppt文件夹下面是资源 [Content_Types].xml 展示是布局和内容说明的入口xml(显示的页面.页面对应的布局.内容.样式文件的路径),里面 ...
- Eclipse项目小红叉
问题:导入自己本子上的项目后,出现小红叉,经检查jar包无误. 原因: 1. 之前电脑和现在电脑上的JDK 版本不一致or JRE 环境不一致,在项目右键菜单Build Path -->conf ...
- A pointer is a variable whose value is the address of another variable 指针 null pointer 空指针 内存地址0 空指针检验
小结: 1.指针的实际值为代表内存地址的16进制数: 2.不同指针的区别是他们指向的变量.常量的类型: https://www.tutorialspoint.com/cprogramming/c_po ...
- malloc函数 链表
https://baike.baidu.com/item/malloc函数 malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void ...
- Page13:跟踪问题、最优控制[Linear System Theory]
内容包含跟踪问题及其结构框图.内模原理.可跟踪条件 各种线性二次型最优控制问题指标含义
- [CentOS7][ssh][publickey][troubleshoot] 通过密钥登录ssh故障排查
通常情况下,我会使用非对称加密的方式来进行ssh的登录. 做法: 将公钥添加到 $HOME/.ssh/authorized_keys 文件中. 但是通常,会遇见各种各样的问题,导致失败.汇总如下: 0 ...
- Python操作Mysql数据库——多表组合查询
前面我们介绍了单张表的查询,包括模糊查询.分组.排序.各种筛选条件等等操作,在实际应用中,查询的数据往往不止局限在一张表里,通常需要多张表在一起进行组合查询,今天我们将会对Mysql当中的多张有关联的 ...
- 2018/04/24 PHP 设计模式之注册树模式
之前学习了工厂模式和单例模式,明白了他们的意义. 但是我们在之后的使用中会发现一个问题,在新建一个实例的时候还是需要调用一个单例或者工厂,之后还是造成了代码和耦合和不好处理. 下面开始说一下: -- ...