Skip to content

Commit 4dadb39

Browse files
committed
feat: first-bad-version
1 parent 37b70b6 commit 4dadb39

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

bts.first-bad-version.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# The isBadVersion API is already defined for you.
2+
# @param version, an integer
3+
# @return an integer
4+
def isBadVersion(version):
5+
pass
6+
7+
class Solution:
8+
"""
9+
278. 第一个错误的版本
10+
https://leetcode-cn.com/problems/first-bad-version/
11+
你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。
12+
由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。
13+
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。
14+
你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。
15+
实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
16+
"""
17+
def firstBadVersion(self, n):
18+
"""
19+
:type n: int
20+
:rtype: int
21+
"""
22+
l, r = 1, n
23+
res = n
24+
while l <= r:
25+
mid = l + (r - l) // 2
26+
if isBadVersion(mid):
27+
res = mid
28+
r = mid - 1
29+
else:
30+
l = mid + 1
31+
32+
return res

0 commit comments

Comments
 (0)