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 ...
随机推荐
- python twisted异步将数据导入到数据库中
from twisted.enterprise import adbapi from twisted.internet import reactor def creat_conn(): # 数据库基本 ...
- VMware 虚拟化编程(1) — VMDK/VDDK/VixDiskLib/VADP 概念简析
目录 目录 VMDK VDDK VixDiskLib VADP VMDK VMDK(VMware's Virtual Machine Disk Format,VMware 虚拟磁盘格式):简单来说就是 ...
- selenium验证码处理之cookie登录
在实际测试中会经常见到登录操作需要验证码验证登录 常见验证有以下几种: 验证码登录 图片识别 图片滑块识别验证 4.简单验证码计算 针对上面的登录验证解决办法有以下几种: 1.让开发去掉验证码 ...
- Java判断一个类里是否存在某个属性
Java判断一个类里是否存在某个属性 测试pojo类,比方我有个User类 @Getter @Setter public class User { private Long id; private S ...
- 事件驱动体系结构(EDA)
- linux 阿里云 centos7 环境下安装easymock(一)
一.说一说Easy-mock的使用场景和优点:1.Easy Mock 是一个可视化,并且能快速生成 模拟数据 的持久化服务,2.基于 Swagger 创建项目,以节省手动创建接口的时间,这点也是我搭建 ...
- ex3 多分类和神经网络
介绍 在本练习中,您将实现一对多逻辑回归和神经识别手写数字的网络.在开始编程之前练习,我们强烈建议观看视频讲座并完成相关主题的复习问题.要开始练习,您需要下载起始代码并将其内容解压缩到要完成练习的目录 ...
- (四:NIO系列) Java NIO Selector
出处:Java NIO Selector 1.1. Selector入门 1.1.1. Selector的和Channel的关系 Java NIO的核心组件包括: (1)Channel(通道) (2) ...
- P4514 上帝造题的七分钟(二维树状数组)
P4514 上帝造题的七分钟 二维树状数组 差分维护区间加法,区间求和 #include<cstdio> int read(){ ,f=; ') f=f&&(c!='-') ...
- 谷歌浏览器安装插件时出现:程序包无效:“CRX_HEADER_INVALID" 后的解决办法
以安装书签管理插件Neater Bookmarks为例: 1.下载:网址http://www.cnplugins.com/zhuanti/sixbookmarksintroduce.html 2.将下 ...