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 ...
随机推荐
- Delphi XE2 之 FireMonkey 入门(9) - TBitmap
TBitmap 主要成员: { 方法 } SetSize(); //设置大小 Clear(); //取消, 就是用指定颜色覆盖 ClearRec ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_04 数据结构_5_数据结构_红黑树
生活中的树和计算机中的树.计算机的树是倒着的
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_07 Collections工具类_1_Collections集合工具类的方法
这是一个个的添加的方式 参数是个可变的元素.可以传递任意多的元素 shuffle打乱集合元素顺序
- 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第4节 等待唤醒机制_1_线程状态概述
Thread中有个嵌套类 它描述了线程的状态 线程状态的图 Object类中的Wait方法和notify方法
- 微服务架构spring cloud - gateway网关限流
1.算法 在高并发的应用中,限流是一个绕不开的话题.限流可以保障我们的 API 服务对所有用户的可用性,也可以防止网络攻击. 一般开发高并发系统常见的限流有:限制总并发数(比如数据库连接池.线程池). ...
- 从Android手机中取出已安装的app包,导出apk
从Android手机中取出已安装的app包,导出apk TAG:Android,提取,apk,adb,pm,root,导出apk 假设有这样一个场景,A君看到你手机上一个实用APP,想要安装到自己手机 ...
- Http Handler 介绍
引言 在 Part.1 Http请求处理流程 一文中,我们了解了Http请求的处理过程以及其它一些运作原理.我们知道Http管道中有两个可用接口,一个是IHttpHandler,一个是IHttpMod ...
- SUST_ACM_2019届暑期ACM集训热身赛题解
问题A:Hello SUST! 知识点:基本输入输出 C/C++: #include <stdio.h> int main() { int n; scanf("%d", ...
- 【题解】Shortest Cycle
原题链接:CF1205B 题目大意 给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...
- ZPL文件打印
ZPL:全称是Zebra Printer Language,目前能够直接打印zpl文件的打印机只有斑马打印机 如何打印zpl文件呢? 软件类: 一.Print Conductor(桌面打印软件) ht ...