Python3解leetcode 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.
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.
思路:主要是学习这个二分法的思路及写法
代码:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
lo , hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
res = isBadVersion(mid)
if res == True:
hi = mid
elif res == False:
lo = mid + 1 return lo
Python3解leetcode First Bad Version的更多相关文章
- Python3解leetcode N-ary Tree Level Order Traversal
问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- Python3解leetcode Linked List Cycle
问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given link ...
- Python3解leetcode Single Number
问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...
- Python3解leetcode 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 ...
- Python3解leetcode Same TreeBinary Tree Level Order Traversal II
问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- Python3解leetcode Same Tree
问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- Python3解leetcode Maximum SubarrayClimbing Stairs
问题: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
随机推荐
- ASP.NET Core 上传微信永久视频素材
话不多说直接上源码 请求实体 public class AddVideoRequest { /// <summary> /// 文件流 / ...
- 阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第1节 继承_1_继承的概述
继承,不要按照父亲和儿子的关系去理解,父亲有100快.那么大儿子就有50 小儿子也50 ,他是对半的 这里要按照师傅和徒弟的关系去理解.师傅会九阴真经 那么徒弟也会九阴真经 程序中的继承 讲师和助教有 ...
- idea多行注释缩进
选中多行代码 - 按下tab键——向后整体移动 选中多行代码 - 按下shift + tab键——向前整体缩进(整体去掉代码前面的空格)
- Flutter修改状态栏颜色以及字体颜色
Flutter沉浸式状态栏 void main() { runApp(MyApp()); if (Platform.isAndroid) { // 以下两行 设置android状态栏为透明的沉浸.写在 ...
- python字典使用总结
作者:python技术人 博客:https://www.cnblogs.com/lpdeboke 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值 key=>value 对用冒号 ...
- Spring Boot & Restful API 构建实战!
作者:liuxiaopeng https://www.cnblogs.com/paddix/p/8215245.html 在现在的开发流程中,为了最大程度实现前后端的分离,通常后端接口只提供数据接口, ...
- Java判断一个日期是否在下周日期区间
Java实现判断一个日期是否在下周日期区间的方法 /** * 判断输入的日期是否在下周区间 * @return * @author nemowang */ public static boolean ...
- HDU 6538 Neko and quadrilateral(极角排序+旋转坐标系)
这道题简直太好了,对于计算几何选手需要掌握的一个方法. 首先对于求解四边形面积,我们可以将四边形按对角线划分成两个三角形,显然此时四边形的面积最大最小值就变成了求解里这个对角线最近最远的点对. 对于此 ...
- 【题解】1-2-K Game
题目大意 现有\(n\)个东西,每次可以取\(1\)个,\(2\)个或\(k\)个.Alice和Bob轮流取,且Alice先取.问谁是最后一个取的.(\(0 \leq n \leq 10^9\), ...
- 第021讲:函数:lambda表达式
0. 请使用lambda表达式将下边函数转变为匿名函数? def fun_A(x, y=): return x * y me:lambda x,y=3:x*y 1.请将下边的匿名函数转变为普通的屌丝函 ...