Skip to content

Commit

Permalink
py & cpp implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Azureki committed Apr 25, 2019
1 parent 735722d commit 90a5438
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 278. First Bad Version/main.cpp
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;
}
};
23 changes: 23 additions & 0 deletions 278. First Bad Version/main.py
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

0 comments on commit 90a5438

Please sign in to comment.