-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Forward declaration of isBadVersion API. | ||
bool isBadVersion(int version); | ||
|
||
class Solution { | ||
public: | ||
int firstBadVersion(int n) { | ||
// 事实上,在最后一般需要判断。但这题确定target存在。 | ||
int left = 1, right = n, mid; | ||
while (left < right) { | ||
mid = (right - left) / 2 + left; | ||
if (isBadVersion(mid)) { | ||
right = mid; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
return left; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# The isBadVersion API is already defined for you. | ||
# @param version, an integer | ||
# @return a bool | ||
# def isBadVersion(version): | ||
|
||
class Solution: | ||
def firstBadVersion(self, n): | ||
""" | ||
:type n: int | ||
:rtype: int | ||
""" | ||
left,right = 0,n | ||
mid = 0 | ||
while left<right: | ||
mid=(left+right)//2 | ||
if isBadVersion(mid): | ||
right = mid | ||
else: | ||
left = mid + 1 | ||
|
||
# 事实上,在最后一般需要判断。但这题确定target存在。 | ||
return left | ||
|