Brush questions website: Leetcode
Difficulty: easy
Language: Python
Plan : From easy -> to medium -> to hard.
1. The first wrong version of 278
1.1 Problem description
Assume that in version development, all versions after the wrong version are wrong. There is n
a version [1,2,...,n]
, if you want to find the first bad version that caused all subsequent versions to fail.
bool isBadVersion(version)
You can judge whether the version number version
is wrong in unit tests by calling the interface. Implementing a function to find the first bad version should minimize the number of calls to the API.
- Example 1
输入:n=5,bad=4
输出:4
解释:
调用 isBadVersion(3) -> false
调用 isBadVersion(5) -> true
调用 isBadVersion(4) -> true
所以,4 是第一个错误的版本。
- Example 2
输入:n = 1, bad = 1
输出:1
1.2 Thinking and Analysis
At the beginning, it is easy to misunderstand the meaning of the title or not understand the title. Note the following two points:
- 1) For example, the version number
[1,2,3,...n]
is expressed in the form of right and wrong[********######]
, which*
means correct and#
wrong version number, and the title requires the position of the first occurrence#
. - 2)
isBadVersion(3) -> false
The meaning of this sentence is not simply tofalse
understand that it is wrong, but toisBadVersion()
judge whether it is wrong. Iffalse
the double negative is equal to affirmation, then this sentence means that the version is correct, otherwiseisBadVersion(4) -> ture
it means that the fourth version number is wrong. of.
After understanding the meaning of the question, we can write the corresponding code. This question is also a binary classification method, similar to the binary search of the previous 704 array .
1.3 Problem solving process
First of all, the console gives the initial code, we just need to write it later.
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return an integer
# def isBadVersion(version):
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
This question has already said that the isBadVersion API
interface is defined, we assume that it already exists, and there is no need to define it ourselves. Therefore, this question cannot be debugged in pycharm, but can only be debugged locally in leetcode.
- (1) First of all, unlike the previous method, the subscript index is not used here, but the version number from 1 to n directly, so we need to define the initial value of the left end to 1
left=1
- (2) Then, we need a loop to determine whether it is the location of the first error. Note that the conditions for the establishment of the loop cannot be written
left<=n
, but should be writtenleft<n
. Otherwise, it will fall into an infinite loop and exceed the time limit, that is, to ensure that there are at least two version numbers.
while left<n:
- (3) Finally, you need to add a judgment statement to call whether the interface is
true
. Initially, I didn't know how to call itisBadVersion() API
, so I directly imitated the title and wrote it
if isBadVersion() -> true:
\qquad But after execution, it always prompts the following error
SyntaxError: invalid syntax
\qquadWhen I ->
changed it to common, I got the ==
following error
NameError:name'ture' is not defined
\qquadLater, after thinking about isBadVersion()
it, it has already been defined, so there should be no problem, that is, there is a problem later. Think about the if
statement after the condition is established, and then execute. Therefore, I simply removed the back and succeeded.
if isBadVersion():
- (4) The final code is as follows
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left = 1
while left<n:
mid = (n-left)//2 + left
if isBadVersion(mid):
n = mid
else:
left = mid + 1
return left
The execution results are as follows:
95.9% of users are defeated, so the algorithm should not be bad. Others may have different configurations for each computer, resulting in different time.
1.4 Summary
The point of this question is to understand the meaning of the question. I didn't understand it at first, but only after reading the comments did I know API
the meaning of calling the interface. The algorithm idea is still the binary search method, which is slightly different from the previous one. It is better to pay more attention and practice more.