首页 > 其他分享 >leetcode-278-easy

leetcode-278-easy

时间:2022-10-31 20:57:37浏览次数:45  
标签:int mid high bad version low easy 278 leetcode

First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example 1:

Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
Example 2:

Input: n = 1, bad = 1
Output: 1
Constraints:

1 <= bad <= n <= 231 - 1

思路一:二分法,因为此题的二分查找里面没有相等的判断条件,所以需要变通一下。看了一下题解,有更好的优化判断

public int firstBadVersion(int n) {
    int high = n;
    int low = 1;

    while (high > low) {
        int mid = (high + low) >>> 1;
        boolean bool = isBadVersion(mid);

        if (bool) {
            high = mid - 1;
            if (!isBadVersion(high)) return mid;
        } else {
            low = mid + 1;
            if (isBadVersion(low)) return low;
        }

    }

    return 1;
}
public static int firstBadVersion(int n) {
    int high = n;
    int low = 1;

    while (high > low) {
        int mid = (high + low) >>> 1;
        boolean bool = isBadVersion(mid);

        if (bool) {
            high = mid;
        } else {
            low = mid;
        }

    }

    return high;
}

标签:int,mid,high,bad,version,low,easy,278,leetcode
From: https://www.cnblogs.com/iyiluo/p/16845751.html

相关文章

  • leetcode-268-easy
    MissingNumberGivenanarraynumscontainingndistinctnumbersintherange[0,n],returntheonlynumberintherangethatismissingfromthearray.Exam......
  • leetcode-500-easy
    KeyboardRowGivenanarrayofstringswords,returnthewordsthatcanbetypedusinglettersofthealphabetononlyonerowofAmericankeyboardliketheim......
  • leetcode-1356-easy
    SortIntegersbyTheNumberOf1BitsYouaregivenanintegerarrayarr.Sorttheintegersinthearrayinascendingorderbythenumberof1'sintheirbinar......
  • AI人脸检测识别EasyCVR视频融合平台告警预案的配置操作与使用
    我们在前期的文章中为大家介绍了EasyCVR新增的告警预案功能,感兴趣的用户可以戳这篇文章:《AI人脸检测智能视频融合平台EasyCVR新增告警预案功能》。  告警预案可以根......
  • EasyCode
    文章目录​​前提准备​​​​1、idea安装easycode插件​​​​本地安装​​​​在线安装(推荐)​​​​2、idea添加数据库​​​​一、easyCode表生成​​​​单表生成:选择......
  • EasyCode
    文章目录​​前提准备​​​​1、idea安装easycode插件​​​​本地安装​​​​在线安装(推荐)​​​​2、idea添加数据库​​​​一、easyCode表生成​​​​单表生成:选择......
  • leetcode-1-easy
    TwoSumGivenanarrayofintegersnumsandanintegertarget,returnindicesofthetwonumberssuchthattheyadduptotarget.Youmayassumethateachinp......
  • C++&Python 描述 LeetCode 1.两数之和
    C++&Python描述LeetCode1.两数之和  大家好,我是亓官劼(qíguānjié),在【亓官劼】公众号、、GitHub、B站、华为开发者论坛等平台分享一些技术博文。放弃不难,但坚持......
  • SpringBoot如何用easyPOI导出excel文件
    在工作中,经常需要我们用Java代码导出一些数据,保存在Excel中。这是非常实用的Excel导出功能,如果我们用SpringBoot结合EasyPOI框架,可以非常方便地实现这个功能。在maven项目中......
  • dp-leetcode152
    动态规划问题,存在重叠子问题/***<p>给你一个整数数组<code>nums</code>&nbsp;,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数......