LintCode Q74 First Bad Version in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2016年12月31日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#class SVNRepo:
# @classmethod
# def isBadVersion(cls, id)
# # Run unit tests to check whether verison `id` is a bad version
# # return true if unit tests passed else false.
# You can use SVNRepo.isBadVersion(10) to check whether version 10 is a
# bad version.
class Solution:
"""
@param n: An integers.
@return: An integer which is the first bad version.
"""
def findFirstBadVersion(self, n):
# write your code here
lo, hi = 1, n
while lo <= hi:
mid = lo + (hi - lo)/2
if SVNRepo.isBadVersion(mid):
hi = mid - 1
res = mid
else:
lo = mid + 1
return res
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。