leetcode-pp / 91alg-5-daily-check

91 天学算法第五期打卡
55 stars 14 forks source link

【Day 38 】2021-10-17 - 278. 第一个错误的版本 #55

Open azl397985856 opened 3 years ago

azl397985856 commented 3 years ago

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 是第一个错误的版本。

wangyifan2018 commented 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
Serena9 commented 3 years ago

代码

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
15691894985 commented 3 years ago

【day 38】278. 第一个错误的版本

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)

xj-yan commented 3 years ago
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)

kennyxcao commented 3 years ago

278. First Bad Version

Intuition

Code

/**
 * @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;
  };
};

Complexity Analysis

okbug commented 3 years ago

思路

这个是二分中的寻找第一个值的案例

代码

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;
    };
};
simonsayshi commented 3 years ago
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;
    }
};
shixinlovey1314 commented 3 years ago

Title:278. First Bad Version

Question Reference LeetCode

Solution

Code

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;
    }
};

Complexity

Time Complexity and Explanation

O(logn)

Space Complexity and Explanation

O(1)

learning-go123 commented 3 years ago

思路

代码

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 为数组长度。

JK1452470209 commented 3 years ago

提示:

思路

二分搜索,查找出右边界第一个错误的版本

代码

    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)

ychen8777 commented 3 years ago

思路

二分查找法

代码

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)

ymkymk commented 3 years ago

思路

二分法

代码

``

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)

biancaone commented 3 years ago
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
Francis-xsc commented 3 years ago

思路

二分法

代码


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;
    }
};

复杂度分析

wenlong201807 commented 3 years ago

代码块

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;
  };
};

时间复杂度和空间复杂度

james20141606 commented 3 years ago

Day 38: 278. First Bad Version (binary search)

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
Bingbinxu commented 3 years ago

思路 二分法:利用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)

laurallalala commented 3 years ago

代码

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

复杂度

JianXinyu commented 3 years ago

思路

典型的二分寻找最左边的满足条件的值。 寻找最左边和寻找指定值的差别就是碰到等于号的处理情况。

二分法

Code

// 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)

chen445 commented 3 years ago

思路

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)

ghost commented 3 years ago

题目

  1. First Bad Version

思路

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)

xjlgod commented 3 years ago
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;
    }
}
Tao-Mao commented 3 years ago

Idea

Binary Search

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 = 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;
    }
}

Complexity

st2yang commented 3 years ago

思路

代码

复杂度

kidexp commented 3 years ago

thoughts

二分法

code

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

Complexity

Time: O(logn)

Space: O(1)

biscuit279 commented 3 years ago

思路:二分法

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)

ai2095 commented 3 years ago

LC278. First Bad Version

https://leetcode.com/problems/first-bad-version/

Topics

Similar Questions

Hard

Medium

思路

binary search

代码 Python

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)

user1689 commented 3 years ago

题目

https://leetcode-cn.com/problems/first-bad-version/

思路

二分区间

python3

# 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

时间复杂度

相关题目

  1. https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/
  2. https://leetcode-cn.com/problems/search-insert-position/
  3. https://leetcode-cn.com/problems/guess-number-higher-or-lower/
AruSeito commented 3 years ago
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)

zol013 commented 3 years ago

标准二分查找

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)

Bochengwan commented 3 years ago

思路

二分 找到最左符合条件的元素。

代码

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

复杂度分析

asterqian commented 3 years ago

思路

最左二分查找,都为闭区间[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;
    }
};

复杂度分析

时间复杂度: O(logn)
空间复杂度: O(1)
HouHao1998 commented 3 years ago

思想

二分法,记住模版万用

代码

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;
    }
}

复杂度

septasset commented 3 years ago

思考

关键点

代码(Python)

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

复杂度分析

RocJeMaintiendrai commented 3 years ago

题目

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)

mixtureve commented 3 years ago

思路

二分法

代码

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 为数组长度。

Cartie-ZhouMo commented 3 years ago

思路

二分法

代码

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

复杂度

Toms-BigData commented 3 years ago

【Day 38】278. 第一个错误的版本

思路

二分法

Python3代码

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)

LareinaWei commented 3 years ago

Thinking

Binary Search.

Code

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

Complexity

Time complexity: O(logn). Space complexity: O(1)

Zhang6260 commented 3 years ago

JAVA版本

思路:

二分法。找到第一个为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)

ysy0707 commented 3 years ago

思路:二分

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)

m-z-w commented 3 years ago

思路:二分,找到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)

Huangxuang commented 3 years ago

题目:278. First Bad Version

思路

代码

/* 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;
    }
}

复杂度分析

ZJP1483469269 commented 3 years ago

题目地址(278. 第一个错误的版本)

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 为数组长度。

brainlds commented 3 years ago
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;

}}

LAGRANGIST commented 3 years ago

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;
} };

babbomax98 commented 3 years ago

思路

二分法:先看中间的值是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)

guangsizhongbin commented 3 years ago

/ 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;
}

}

liuyangqiQAQ commented 3 years ago

二分查找效率相对于直接遍历高。所以采取二分

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;
    }
}
peteruixi commented 3 years ago

思路

二分法来缩小边界, 假如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;
    }
}

复杂度分析