Open azl397985856 opened 3 years ago
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left <= right:
mid = left + (right - left) // 2
if isBadVersion(mid) == False:
left = mid + 1
else:
right = mid - 1
return left
class Solution:
def firstBadVersion(self, n):
l, r = 1, n
while l <= r:
mid = (l + r) // 2
if isBadVersion(mid):
# 收缩
r = mid - 1
else:
l = mid + 1
return l
https://leetcode-cn.com/problems/first-bad-version
思路:第一个找到,就是一个最左二分,能力检测 函数isBadVersion() 相当于定义posiible
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left = 0
right = n
while left <= right:
mid = (right + left)//2
#写 mid = math.ceil(left+ (right -left)/2 )计算时间要长一点
if isBadVersion(mid):
right = mid -1
else:
left = mid +1
return left
复杂度:
时间复杂度:O(log n)
空间复杂度:O(1)
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 1, right = n;
while (left <= right){
int mid = (right - left) / 2 + left;
if (isBadVersion(mid)) right = mid - 1;
else left = mid + 1;
}
return left;
}
}
Time Complexity: O(logN), Space Complexity: O(1)
/**
* @param {function} isBadVersion()
* @return {function}
*/
const solution = function(isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function(n) {
let left = 1;
let right = n;
while (left < right) {
const mid = left + ~~((right - left) / 2);
if (isBadVersion(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
};
这个是二分中的寻找第一个值的案例
var solution = function(isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function(n) {
let left = 1, right = n;
while (left <= right) {
let mid = left + ((right - left) >> 1);
if (isBadVersion(mid)) right = mid - 1;
else left = mid + 1;
}
return left;
};
};
class Solution {
public:
int firstBadVersion(int n) {
int start = 0, end = n;
int mid;
while(start+1<end)
{
mid = start+(end-start)/2;
if(isBadVersion(mid))
end = mid;
else
start= mid;
}
if(isBadVersion(start))
return start;
return end;
}
};
class Solution {
public:
int firstBadVersion(int n) {
int l = 1, r = n;
while (l <= r) {
int mid = l + (r - l) / 2;
if (isBadVersion(mid))
r = mid - 1;
else
l = mid + 1;
}
return l;
}
};
O(logn)
O(1)
Go Code:
/**
* Forward declaration of isBadVersion API.
* @param version your guess about first bad version
* @return true if current version is bad
* false if current version is good
* func isBadVersion(version int) bool;
*/
func firstBadVersion(n int) int {
left := 1
for left < n {
mid := (n-left)/2 + left
if isBadVersion(mid) {
n = mid
} else {
left = mid + 1
}
}
return left
}
复杂度分析
令 n 为数组长度。
提示:
思路
二分搜索,查找出右边界第一个错误的版本
代码
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 0,right = n;
while(left < right){
int mid = left + (right - left) / 2;
if(this.isBadVersion(mid)){
right = mid;
}else{
left = mid + 1;
}
}
return right;
}
}
复杂度
时间复杂度:O(logN)
空间复杂度:O(1)
二分查找法
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int l = 1, r = n, mid = -1
while(l < r){
mid = l + (r - l) / 2;
if(isBadVersion(mid)){
r = mid;
}else{
l = mid+1;
}
}
return l;
}
}
时间: O(log n) \ 空间: O(1)
二分法
``
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 1, right = n;
while (left < right) { // 循环直至区间左右端点相同
int mid = left + (right - left) / 2; // 防止计算时溢出
if (isBadVersion(mid)) {
right = mid; // 答案在区间 [left, mid] 中
} else {
left = mid + 1; // 答案在区间 [mid+1, right] 中
}
}
// 此时有 left == right,区间缩为一个点,即为答案
return left;
}
}
时间复杂度:O(logn)
空间复杂度:O(1)
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left + 1 < right:
mid = (left + right) // 2
if isBadVersion(mid):
right = mid
else:
left = mid
if isBadVersion(left):
return left
return right
二分法
class Solution {
public:
int firstBadVersion(int n) {
int l=1,r=n;
while(l<r)
{
int mid=l+((r-l)>>1);
if(isBadVersion(mid))
r=mid;
else
l=mid+1;
}
return l;
}
};
复杂度分析
var solution = function (isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function (n) {
let left = 1,
right = n;
while (left < right) {
const mid = Math.floor((right - left) / 2 + left);
if (isBadVersion(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
};
Problem Link
Ideas
equal
condition. For leftmost scenario it equals to shrink the right boundary. (and vice versa). For looking for the specific value scenario we just return the valueComplexity: hash table and bucket
Code
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 0, n - 1
while left <= right:
middle = (left + right) // 2
print (middle)
if isBadVersion(middle):
if isBadVersion(middle + 1) == False: #the double if is not necessary!
return middle + 1
right = middle - 1
else:
left = middle + 1
return left
class Solution:
def firstBadVersion(self, n):
l, r = 1, n
while l <= r:
mid = (l + r) // 2
if isBadVersion(mid):
# 收缩
r = mid - 1
else:
l = mid + 1
return l
思路 二分法:利用false和true作为分界线,左加右减 代码C++
class Solution {
public:
int firstBadVersion(int n) {
int left = 1;
int right = n;
while(left <= right)
{
int middle = left + (right - left) / 2;
if(isBadVersion(middle) == false)
{
left = middle + 1;
}
else if(isBadVersion(middle) == true)
{
right = middle - 1;
}
}
return left;
}
};
复杂度 时间复杂度:二分次数O(logN) 空间复杂度:O(1)
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
low, up = 0, n
while low<up:
mid = (low+up) / 2
isBad = isBadVersion(mid)
if isBad:
up = mid
else:
low = mid + 1
return low
典型的二分寻找最左边的满足条件的值。 寻找最左边和寻找指定值的差别就是碰到等于号的处理情况。
二分法
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int l = 1, r = n;
while(l < r){
int mid = l + (r - l) / 2;
if(isBadVersion(mid)){
r = mid;
}else{
l = mid + 1;
}
}
return l;
}
};
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
if(n <= 1)
return n;
int l = 1, r = n;
while(l <= r){
int mid = l + ((r - l)>>1);
if(isBadVersion(mid))
r = mid-1;
else
l = mid+1;
}
return l;
}
};
T: O(logn) S: O(1)
Using binary search
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left=0
right=n
while left<right:
mid=left+(right-left)//2
if isBadVersion(mid):
right=mid
else:
left=mid+1
return left
Time: O(logn)
Space: O(1)
Binary Search
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
l, r = 1, n
while(l <= r):
mid = (l+r)//2
if isBadVersion(mid):
r = mid-1
else:
l = mid+1
return l
Space: O(1) Time: O(logn)
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 0, right = n;
while (left <= right) {
int mid = (right - left) / 2 + left;
boolean isBad = isBadVersion((int) mid);
if (isBad == false) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
}
Binary Search
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 1;
int right = n;
int mid = (right - left)/2 + left;
while (left < right) {
boolean tmp = isBadVersion(mid);
if (!isBadVersion(mid-1) && tmp) {
return mid;
}
else if (!tmp) {
left = mid + 1;
}
else {
right = mid - 1;
}
mid = (right - left)/2 + left;
}
return mid;
}
}
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 0, n - 1
while left <= right:
mid = (left + right) // 2
if isBadVersion(mid):
right = mid - 1
else:
left = mid + 1
return left
二分法
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
start, end = 1, n
while start < end:
mid = (start + end) // 2
if isBadVersion(mid):
end = mid
else:
start = mid + 1
return end
Time: O(logn)
Space: O(1)
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
l,r = 1,n
ans = 0
while l<=r:
mid = (l+r)//2
if isBadVersion(mid):
ans = mid
r = mid -1
else:
l = mid + 1
return ans
时间复杂度:O(logn) 空进复杂度:O(1)
https://leetcode.com/problems/first-bad-version/
Hard
Medium
binary search
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
if isBadVersion(1):
return 1
left, right = 1, n
while left <= right:
mid = left + (right - left) // 2
if isBadVersion(mid):
if not isBadVersion(mid-1):
return mid
else:
right = mid -1
if not isBadVersion(mid):
if isBadVersion(mid+1):
return mid+1
else:
left = mid + 1
return right
时间复杂度: O(logn) 空间复杂度:O(1)
https://leetcode-cn.com/problems/first-bad-version/
二分区间
# 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
"""
left, right = 1, n
while (left < right):
mid = left + (right - left) // 2
# if method return true, it means we find a bad version.
if isBadVersion(mid):
# L M R
# [1,2,3,4,5,6]
# 写right = mid,
# 当right收缩时 当mid位置元素是bad是不可能错过mid对应的值 即使bad一定存在[L,R]区间内
# 作为对比写成[L,R-1]就可能不包含bad元素即不符合题意
right = mid
else:
left = mid + 1
return right
var solution = function(isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function(n) {
let start = 1, end = n,mid = parseInt((n+1)/2);
while(start<=end){
if(isBadVersion(mid)){
end = mid - 1;
mid = parseInt((start+end)/2)
}else{
start = mid + 1;
mid = parseInt((start+end)/2)
}
}
return start;
};
};
O(log n) O(1)
标准二分查找
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
l, r = 1, n
while l <= r:
mid = (l + r) // 2
if isBadVersion(mid):
r = mid - 1
else:
l = mid + 1
return l
TC: O(logn) SC: O(1)
二分 找到最左符合条件的元素。
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
l, r= 0,n-1
while l<=r:
mid = (r+l)//2
if isBadVersion(mid):
r = mid-1
else:
l = mid+1
return l
复杂度分析
最左二分查找,都为闭区间[left, right]。如果调用API为true,则应该往左边找,如果为false证明左边的都是false应该往右边找。
class Solution {
public:
int firstBadVersion(int n) {
int l = 1;
int r = n;
int ans = -1;
while (l <= r) {
int mid = l + (r - l) / 2;
// true, check left half
if (isBadVersion(mid)) {
ans = mid;
r = mid - 1;
} else {
// check right half
l = mid + 1;
}
}
return ans;
}
};
二分法,记住模版万用
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int l=0,r=n,ans=0;
while (l<=r){
int mid= l+(r-l)/2;
if(isBadVersion(mid)){
ans= mid;
r = mid-1;
}else {
l= mid+1;
}
}
return ans;
}
}
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
l = 1
r = n
while l<=r:
mi = (l+r) // 2
if not isBadVersion(mi):
l = mi + 1
else:
if mi == 1: return mi
if not isBadVersion(mi - 1):
return mi
else:
r = mi - 1
return -1
https://leetcode-cn.com/problems/first-bad-version/
Binary Search
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int start = 1;
int end = n;
while(start < end) {
int mid = (end - start) / 2 + start;
if(isBadVersion(mid)) {
end = mid;
} else {
start = mid + 1;
}
}
return end;
}
}
O(logN)
O(1)
二分法
Java Code:
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 0;
int right = n;
while (left < right - 1) {
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return isBadVersion(left)? left: right;
}
}
复杂度分析
令 n 为数组长度。
二分法
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
l,r = 1, n
while l < r:
mid = (l + r) // 2
if isBadVersion(mid):
r = mid
else:
l = mid + 1
return l
复杂度
二分法
class Solution:
def firstBadVersion(self, n):
if n == 1:
return 1
l = 0
r = n
mid = int(n/2)
while not (isBadVersion(mid) == False and isBadVersion(mid+1) == True):
if isBadVersion(mid) == False:
l = mid
mid = int((l + r)/2)
else:
r=mid
mid = int((l + r)/2)
return mid+1
时间:O(logn) 空间:O(1)
Binary Search.
class Solution:
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
l = 1
r = n
while l <= r:
mid = l + (r - l) // 2
if isBadVersion(mid) == False:
l = mid + 1
else:
r = mid - 1
return l
Time complexity: O(logn). Space complexity: O(1)
二分法。找到第一个为ture的。
public class Solution extends VersionControl { public int firstBadVersion(int n) { int a=1,mid=0,b=n; while(a<=b){ mid=(b-a)/2+a; if(isBadVersion(mid)){ b=mid-1; }else{ a=mid+1; } } return a; } }
时间复杂度:O(logn)
空间复杂度:O(1)
思路:二分
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int l = 0, r = n;
while(l <= r){
int mid = l +(r - l) / 2;
if(!isBadVersion(mid)){
l = mid + 1;
}else{
r = mid - 1;
}
}
return l;
}
}
时间复杂度:O(logN) 空间复杂度:O(1)
思路:二分,找到isBadVersion为true后要继续想左找,直到找到跳出循环,此时left就是返回值
var solution = function(isBadVersion) {
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function(n) {
let left = 1
let right = n
while (left <= right) {
let mid = Math.floor(left + (right - left) / 2)
if (isBadVersion(mid)) {
right = mid - 1
} else {
left = mid + 1
}
}
return left
};
};
时间复杂度:O(logn) 空间复杂度:O(1)
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 1, right = n;
while (left + 1 < right) {
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) {
right = mid;
} else {
left = mid;
}
}
if (isBadVersion(left)) {
return left;
}
return right;
}
}
https://leetcode-cn.com/problems/first-bad-version/
你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。
你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
示例 1:
输入:n = 5, bad = 4
输出:4
解释:
调用 isBadVersion(3) -> false
调用 isBadVersion(5) -> true
调用 isBadVersion(4) -> true
所以,4 是第一个错误的版本。
示例 2:
输入:n = 1, bad = 1
输出:1
提示:
1 <= bad <= n <= 231 - 1
二分寻找第一个错误版本
Java Code:
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int l=0,r=n;
while(l<=r){
int mid = l+(r-l)/2;
if(!isBadVersion(mid)) l = mid +1;
else r = mid - 1;
}
return l;
}
}
复杂度分析
令 n 为数组长度。
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int l=0;
int r=n;
int mid =0;
while(l<r){
mid = l+(r-l)/2;
if(!isBadVersion(mid)){
l = mid+1;
}else{
r = mid;
}
}
return l;
}}
class Solution {
public:
int firstBadVersion(int n) {
int left = 0;
int right = n;
while (left < right)
{
int mid = left + (right - left) / 2;
if (isBadVersion(mid) == true)
right = mid;
else left = mid + 1;
}
return left;
}
};
二分法:先看中间的值是true还是false,如果是true,则在【0,mid】区间,否则在【mid+1,n】区间
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 0;
int right = n;
while(left < right){
int mid = (right - left) / 2 + left;
if (isBadVersion(mid)){
right = mid;
}else{
left = mid + 1;
}
}
return right;
}
}
时间复杂度:O(logn)
/ The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); /
public class Solution extends VersionControl { public int firstBadVersion(int n) { int l = 1; int r = n; int ans = -1;
while(l < r){
int mid = l + (r - l) / 2;
if (isBadVersion(mid)){
r = mid;
}else {
l = mid + 1;
}
}
// l 与 r 均可
// return l;
return r;
}
}
class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 0, right = n;
while (left < right) {
int mid = left + (right - left) / 2;
if(isBadVersion(mid)) {
right = mid;
}else {
left = mid + 1;
}
}
return right;
}
}
二分法来缩小边界, 假如true 缩小左边 否则缩小左边
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int start = 0, end = n;
while(start< end){
int mid = start+(end-start)/2;
if(isBadVersion(mid)){
end = mid;
}
else{
start = mid +1;
}
}
return start;
}
}
278. 第一个错误的版本
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/first-bad-version
前置知识
题目描述
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。
你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
示例:
给定 n = 5,并且 version = 4 是第一个错误的版本。
调用 isBadVersion(3) -> false 调用 isBadVersion(5) -> true 调用 isBadVersion(4) -> true
所以,4 是第一个错误的版本。