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 ...
随机推荐
- C# winform OpenFileDialog用法
https://jingyan.baidu.com/article/e52e36156fa6d240c60c51c8.html 详情看看这个.
- VMware 虚拟化编程(2) — 虚拟磁盘文件类型详解
目录 目录 前文列表 虚拟磁盘文件 VMDK 用户可以创建的虚拟磁盘类型 VixDiskLib 中支持的虚拟磁盘类型 虚拟机文件类型 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/ ...
- import * as 用法
- python学习笔记(数据类型)
python数据类型: int 类型 float 小数类型 string 字符串 布尔类型 a = True b = False 1.列表,也称数组或list或array.它的表达方式通过下标或索引或 ...
- EL表达式.jsp指令等
1.JSP标准指令:<%@ 标准指令(属性 )%><%@ page %><%@ include %><%@ taglib %> 2.JSP程序代码元素: ...
- 为什么说 Babel 将推动 JavaScript 的发展【转】
Babel是一个转换编译器,它能将 ES6 转换成可以在浏览器中运行的代码.Babel 由来自澳大利亚的开发者Sebastian McKenzie创建.他的目标是使 Babel 可以处理 ES6 的所 ...
- 管理MySQL 从入门到出门
MySQL 中的数据库(Database)就像是一个容器,其中包含了各种对象.例如,数据表(Table).视图(View).存储过程(Stored Procedure)以及触发器(Trigger)等. ...
- 前端 CSS的选择器 高级选择器
高级选择器分为: 后代选择器 儿子选择器 并集选择器 交集选择器 后代选择器 使用空格表示后代选择器.父元素的后代(包括儿子,孙子,重孙子) 后代选择器 在CSS中使用非常频繁 因为HTML元素可以嵌 ...
- 前端 CSS层叠性 CSS选择器优先级
层叠性 层叠性:权重的标签覆盖掉了权重小的标签,说白了 ,就是被干掉了 权重:谁的权重大,浏览器就会显示谁的属性 我们现在已经学过了很多的选择器,也就是说在一个HTML页面中有很多种方式找到一个元素并 ...
- 日记smarthome
测试命令:测试命令 7e 7e 两个字节 一个字节 两个字节 一个字节 解释: 两个字节是userid的值 int Userid = data[i] * 256 + data[i + 1]; ...