• 力扣练习,假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。


    java中的代码

        public static int firstBadVersion(int n) {
            int low = 0;
            int high = n;
            while (low < high) {
                int mid = low / 2 + (high - low) / 2; // 直接调用(high + low) / 2求均值会存在越界的问题
                if (isBadVersion(mid)) {
                    high = mid;
                } else {
                    low = mid + 1;
                }
            }
            return low;
        }
     

    python1的代码

    # The isBadVersion API is already defined for you.

    # def isBadVersion(version: int) -> bool:

    class Solution:

        def firstBadVersion(self, n: int) -> int:

            low,high = 0,n

            while(low < high):

                mid = low /2 + (high - low )/2

                if(isBadVersion(mid)):

                    high = mid 

                else:

                    low = mid + 1

            return low

    超出时间限制

    java中的代码

    /* 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 low=1,high=n;
            while(low<=high){
                if(n==1){return 1;}
                int mid=low+(high-low)/2;
                if(isBadVersion(mid)){
                    high=mid;
                }
                else{
                    low=mid+1;
                }
                if(low==high){return high;}
        }
            return high+1;
            }
    }

    python代码
    # The isBadVersion API is already defined for you.
    # @param version, an integer
    # @return a bool
    # def isBadVersion(version):

    class Solution(object):
        def firstBadVersion(self, n):
            """
            :type n: int
            :rtype: int
            """
            a = 0
            i = n//2
            # 临界点是a和n版本相邻,且右版本大于左版本
            while not(n-a == 1): 
                if isBadVersion(i):
                    # n是右值
                    n = i 
                else:
                    # a是左值
                    a = i 
                # i是中值
                i = (a + n) // 2
            # 最后一个i=(a+n)//2=a,此时a是最后一个flase,n是第一个true
            #所以我们需要返回n,n=a+1=i+1
            return i + 1


    作者:i3rave-rubin74b
    链接:https://leetcode.cn/problems/first-bad-version/solution/by-i3rave-rubin74b-8w2c/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

     

    # The isBadVersion API is already defined for you.

    # def isBadVersion(version: int) -> bool:

    class Solution:

        def firstBadVersion(self, n: int) -> int:

            a = 0

            i = n//2

            #临界点时a和n版本相邻,且由版本大于左版本

            while not(n-a == 1):

                if isBadVersion(i):

                    #n是右值

                    n = i

                else:

                    #a是左值

                    a = i

                #i是中值

                i = (a + n) // 2

            #最后一个i =(a + n)//2 = a,此时a是最后一个flase,n是第一个true,所以我们需要返回n , n = a + 1 = i + 1

            return i + 1

  • 相关阅读:
    三川智控定时控制开关灯
    食品饮料行业B2B商城系统:加速行业数字化转型,提升B2B平台交易效率
    【项目_03】日历的回显、搭建热门精选、下拉加载更多、搜索框搭建 | 基于Vue3全家桶
    基于springboot操作clickhouse进行大数据查询和批量插入
    【Lilishop商城】No2-1.确定项目结构和数据结构(用户、商品、订单、促销等模块)
    减法器的设计与实现并用译码器显示16、10进制
    真的假的?NVIDIA研究在人工智能帮助下瞬间将2D照片转化为3D场景
    【兔子王赠书第3期】《案例学Python(进阶篇)》
    红队专题-从零开始VC++C/S远程控制软件RAT-MFC-超级终端
    redis基础知识总结——数据类型(字符串,列表,集合,哈希,集合)
  • 原文地址:https://blog.csdn.net/m0_51919640/article/details/127975612