Lintcode: First Bad Version 解题报告
First Bad Version
http://lintcode.com/en/problem/first-bad-version
The code base version is an integer and start from 1 to n. One day, someone commit a bad version in the code case, so it caused itself and the following versions are all failed in the unit tests.
You can determine whether a version is bad by the following interface:
Java:
public VersionControl {
boolean isBadVersion(int version);
}
C++:
class VersionControl {
public:
bool isBadVersion(int version);
};
Python:
class VersionControl:
def isBadVersion(version)
Find the first bad version.
You should call isBadVersion as few as possible.
Please read the annotation in code area to get the correct way to call isBadVersion in different language. For example, Java is VersionControl.isBadVersion.
Given n=5
Call isBadVersion(3), get false
Call isBadVersion(5), get true
Call isBadVersion(4), get true
return 4 is the first bad version
Do not call isBadVersion exceed O(logn) times.
Tags Expand

SOLUTION 1:
/**
* public class VersionControl {
* public static boolean isBadVersion(int k);
* }
* you can use VersionControl.isBadVersion(k) to judge wether
* the kth code version is bad or not.
*/
class Solution {
/**
* @param n: An integers.
* @return: An integer which is the first bad version.
*/
public int findFirstBadVersion(int n) {
// write your code here
if (n == 1) {
return 1;
} int left = 1;
int right = n; while (left + 1 < right) {
int mid = left + (right - left) / 2;
if (VersionControl.isBadVersion(mid)) {
right = mid;
} else {
left = mid;
}
} if (VersionControl.isBadVersion(left)) {
return left;
} return right;
}
}
SOLUTION 2:
也可以简化一点儿:
/**
* public class VersionControl {
* public static boolean isBadVersion(int k);
* }
* you can use VersionControl.isBadVersion(k) to judge wether
* the kth code version is bad or not.
*/
class Solution {
/**
* @param n: An integers.
* @return: An integer which is the first bad version.
*/
public int findFirstBadVersion(int n) {
// write your code here
if (n == 1) {
return 1;
} int left = 1;
int right = n; while (left < right) {
int mid = left + (right - left) / 2;
if (VersionControl.isBadVersion(mid)) {
right = mid;
} else {
left = mid + 1;
}
} return right;
}
}
Lintcode: First Bad Version 解题报告的更多相关文章
- 【LeetCode】278. First Bad Version 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.c ...
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- Lintcode: Longest Common Substring 解题报告
Longest Common Substring 原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/# Given tw ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- Lintcode: Kth Largest Element 解题报告
Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...
- pat1001. Battle Over Cities - Hard Version 解题报告
/**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+ ...
- codeforces B. Ping-Pong (Easy Version) 解题报告
题目链接:http://codeforces.com/problemset/problem/320/B 题目意思:有两种操作:"1 x y" (x < y) 和 " ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
随机推荐
- Zabbix Trigger表达式实例
Zabbix提供强大的触发器(Trigger)函数以方便进行更为灵活的报警及后续动作,具体触发器函数可以访问https://www.zabbix.com/documentation/2.0/manua ...
- Webdriver设置Firefox自动下载
看到很多人在firefox上操作下载窗口用autoit,太麻烦了,提供一种用profile来解决的办法: FirefoxProfile profile = new FirefoxProfile(); ...
- 树莓派进阶之路 (018) - raspberryPi摄像头命令行软件raspistill帮助文档
文档原文:https://www.raspberrypi.org/app/uploads/2013/07/RaspiCam-Documentation.pdf 作用 在指定的时间运行摄像头,而且如果需 ...
- Android之listview运用(美团美食列表)
首先我们将listview简单实现,有图形,有文字:效果如图 之前我们完成了一个较为简单的listview视图列表,但是生活中我们往往碰到的 是更为复杂列表,有图像有评分标准,不如我们来试一试手,做一 ...
- linux记录sftp命令
使用以下配置方法不需要配置chroot. 编辑sshd_config文件 vi /etc/ssh/sshd_config 增加: Subsystem sftp /usr/libexec/openssh ...
- 跟我学SharePoint 2013视频培训课程——怎样创建文档库并上传文档(8)
课程简介 第8天,怎样在SharePoint 2013怎样创建文档库并上传文档. 视频 SharePoint 2013 交流群 41032413
- netty LEAK: ByteBuf.release() was not called before it's garbage-collected
背景.netty抛出完整的error信息如下: 2018-02-08 14:30:43.098 [nioEventLoopGroup-5-1] ERROR io.netty.util.Resource ...
- [转]SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- django admin 根据choice字段选择的不同来显示不同的页面
一.举例 tip/tip.js var react = function () { if (django.jQuery('#id_tiptype').val() == 'content') { dja ...
- 微信小程序 开发过程中遇到的坑(一)
2124 1.我们使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 的时候在pages中写注释的时候回报错. 例如: { &quo ...