[LeetCode]--278. 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 will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

其实很简单,别想复杂了。isBadVersion(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 low = 1, high = n;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (isBadVersion(mid))
                high = mid;
            else
                low = mid + 1;
        }
        return low;
    }
}
时间: 2024-10-17 02:51:09

[LeetCode]--278. First Bad Version的相关文章

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode] Compare Version Numbers

Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . charac

[LeetCode]165.Compare Version Numbers

[题目] Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . c

[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 b

[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 ve

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

leetcode 10 Regular Expression Matching(简单正则表达式匹配)

最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,python主要为了后序转型数据分析和机器学习,所以今天来做一个难度为hard 的简单正则表达式匹配. 做了很多leetcode题目,我们来总结一下套路: 首先一般是检查输入参数是否正确,然后是处理算法的特殊情况,之后就是实现逻辑,最后就是返回值. 当编程成为一种解决问题的习惯,我们就成为了一名纯粹的程序

刷leetcode是什么样的体验?【转】

转自:https://www.zhihu.com/question/32322023   刷leetcode是什么样的体验? https://leetcode.com/ 1 条评论   默认排序 按时间排序 75 个回答   糊你熊脸 鹰的眼睛!熊的力量!鱼的记忆! 71 人赞同 找工作那段闹心的日子里看书看累了?刷几题吧-心慌气短压力大?刷几题吧-不知道要做啥?还是刷几题吧-居家旅行,缓解压力,清空罪槽必备良药- 刷Leetcode的主要作用,在我看来,其实是为了维持一种编程状态. 小生在某小

[LeetCode] Minimum Genetic Mutation 最小基因变化

A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T". Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE m