First Bad Version——LeetCode
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.
题目大意:n个版本,如果某个版本是坏的,后面都是坏的,找出第一个坏的。
解题思路:二分查找。
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
if (isBadVersion(1)){
return 1;
}
int lo = 0;
int hi = n - 1;
while (lo <= hi) {
int mid = (lo + hi) >>> 1;
if (!isBadVersion(mid + 1) && isBadVersion(mid + 2)) {
return mid + 2;
}
if (mid > 0 && !isBadVersion(mid) && isBadVersion(mid + 1)) {
return mid + 1;
} else if (isBadVersion(mid + 1)) {
hi = mid - 1;
} else if (!isBadVersion(mid + 1)) {
lo = mid + 1;
}
}
return -1;
}
}
First Bad Version——LeetCode的更多相关文章
- First Bad Version leetcode java
问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- First Bad Version - LeetCode
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- 278. First Bad Version - LeetCode
Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...
- [LeetCode] First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- LeetCode Compare Version Numbers
原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- LeetCode算法题-First Bad Version(Java实现-三种解法)
这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
随机推荐
- 9.21 noip模拟试题
Problem 1 护花(flower.cpp/c/pas) [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时 ...
- cocos2dx 各种环境的搭建
http://www.cocos.com/doc/tutorial/index?type=cocos2d-x Windows7上搭建Cocos2d-x 3.4开发环境 这里需要注意的是,如果是搭建VS ...
- 利用linq快速判断给定数字是否包含在某个段范围内
一.需求: 知道某段范围0x0020~0x007F0x00A0~0x017F0x01A0~0x01CF0x01F0~0x01FF0x0210~0x021F0x1EA0~0x1EFF给定一个值,快速判断 ...
- 分享最近写的 两条sql语句
1. 搭建基本环境 插入测试数据 insert into jgdm (jgdm,jgmc) values('12300000000','河南省');insert into jgdm (jgdm,jg ...
- os项目icon和default 等相关图标命名规则和大小设置
最新的参考apple官网地址:https://developer.apple.com/library/ios/qa/qa1686/_index.html,网页下面有详细的使用方法(ios7以后的) 转 ...
- iOS中JavaScript和OC交互
转载自:http://www.devzeng.com/blog/ios-uiwebview-interaction-with-javascript.html 还可参考的文章:http://blog.c ...
- arm Linux 系统调用过程
系统调用是操作系统提供给用户(应用程序)的一组接口,每个系统调用都有一个对应的系统调用函数来完成相应的工作.用户通过这个接口向操作系统申请服务,如访问硬件,管理进程等等.但是因为用户程序运行在用户空间 ...
- Java学习----对象间的继承
继承:子类可以使用父类非私有的成员变量和方法 public class Father { public String name; public String bloodType; private in ...
- HTML5-黑客帝国2D
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...
- php生成缩略图
<?php /** * 生成缩略图函数(支持图片格式:gif.jpeg.png和bmp) * @author ruxing.li * @param string $src 源图片路径 * @pa ...