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 ...
随机推荐
- nacos 使用 servlet 异步处理客户端配置长轮询
config 客户端 ClientWorker#ClientWorker 构造方法中启动定时任务 ClientWorker.LongPollingRunnable 长轮询的任务,在 run 方法的结尾 ...
- import * as 用法
- 必须Mark!43个优秀的Swift开源项目推荐(转)
作为一门集百家之长的新语言,Swift拥有着苹果先天的生态优势,而其在GitHub上各种优秀的开源项目也层出不穷.本文作者@SwiftLanguage从2014年6月苹果发布Swift语言以来,便通过 ...
- 16/7/7_PHP-方法重载
PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的.属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值.读取.判断属性是否设置.销毁属性. ...
- MySQL Cluster 与 MongoDB 复制群集分片设计及原理
分布式数据库计算涉及到分布式事务.数据分布.数据收敛计算等等要求 分布式数据库能实现高安全.高性能.高可用等特征,当然也带来了高成本(固定成本及运营成本),我们通过MongoDB及MySQL Clus ...
- python实现建立tcp通信
实现代码如下: #tcp协议通信import socketclass TcpConnect: def get_tcp(self,ip,port,message): #实例化一个基于tcp的socket ...
- airtest自动化中的poco+python连接手机实现ui自动化
airtest:http://airtest.netease.com/docs/docs_AirtestIDE-zh_CN/index.html官网地址 AirtestIDE:跨平台的UI自动化测试编 ...
- 关于java的数组
一定要写成 int[] arr = new int[30] 这样每个元素默认为0; 介样子的 如果写成 int[] arr = {1,2,3,4}; 那么他的长度就是4
- python 定义模块作用及分类
python把一个功能的模块归类,简单来说,模块是一个由Python代码组成的文件.模块可以定义函数,类和变量. 模块还可以包括可运行的代码. 1,python模块的作用 提高代码的方便维护 使用模块 ...
- Vue.js——60分钟快速入门 一
来源:https://www.cnblogs.com/keepfool/p/5619070.html Vue.js介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组 ...