lintcode :First bad version 第一个错误的代码版本
题目
第一个错误的代码版本
代码库的版本号是从 1 到 n 的整数。某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错。请找出第一个错误的版本号。
你可以通过 isBadVersion 的接口来判断版本号 version 是否在单元测试中出错,具体接口详情和调用方法请见代码的注释部分。
给出 n=5
调用isBadVersion(3),得到false
调用isBadVersion(5),得到true
调用isBadVersion(4),得到true
此时我们可以断定4是第一个错误的版本号
请阅读上述代码,对于不同的语言获取正确的调用 isBadVersion 的方法,比如java的调用方式是SVNRepo.isBadVersion
调用 isBadVersion 的次数越少越好
解题
感觉顺序遍历,找到第一个出现true的就是答案了,但是超时了。只有二分查找了
/**
* public class SVNRepo {
* public static boolean isBadVersion(int k);
* }
* you can use SVNRepo.isBadVersion(k) to judge whether
* 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<=0)
return 0;
if(n==1)
return 1;
int left = 1;
int right = n;
while(left < right){
int mid = (left + right)/2;
if(SVNRepo.isBadVersion(mid))
right = mid;
else
left = mid + 1;
}
return left;
}
}
Java Code
#class SVNRepo:
# @classmethod
# def isBadVersion(cls, id)
# # Run unit tests to check whether verison `id` is a bad version
# # return true if unit tests passed else false.
# You can use SVNRepo.isBadVersion(10) to check whether version 10 is a
# bad version.
class Solution:
"""
@param n: An integers.
@return: An integer which is the first bad version.
"""
def findFirstBadVersion(self, n):
# write your code here
if n<=0:return 0
if n==1:return 1
l = 1
r = n
while l< r:
m = (l + r)/2
if SVNRepo.isBadVersion(m):
r = m
else:
l = m + 1
return r
Python Code
lintcode :First bad version 第一个错误的代码版本的更多相关文章
- [LintCode] 第一个错误的代码版本
/** * class VersionControl { * public: * static bool isBadVersion(int k); * } * you can use VersionC ...
- lintcode-74-第一个错误的代码版本
74-第一个错误的代码版本 代码库的版本号是从 1 到 n 的整数.某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错.请找出第一个错误的版本号. 你可以通过 isBad ...
- 278 First Bad Version 第一个错误的版本
你是产品经理,目前正在领导一个团队开发一个新产品.不幸的是,您的产品的最新版本没有通过质量检查.由于每个版本都是基于之前的版本开发的,所以错误版本之后的所有版本都是不好的.假设你有 n 个版本 [1, ...
- java-Unsupported major.minor version 52.0错误解决
java-Unsupported major.minor version 52.0错误解决 eclipse版本设置不对, 低版本不能兼容高版本 eclipse中: windows -> pref ...
- [Java]LeetCode278. 第一个错误的版本 | First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version)
Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version) 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版 ...
- LeetCode 278. 第一个错误的版本(First Bad Version)
278. 第一个错误的版本 LeetCode278. First Bad Version 题目描述 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每 ...
- 【leetcode 简单】 第七十五题 第一个错误的版本
你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. 假设你有 n 个版本 [1, ...
- LeetCode初级算法--排序和搜索01:第一个错误的版本
LeetCode初级算法--排序和搜索01:第一个错误的版本 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.cs ...
随机推荐
- UIBezierPath 的使用介绍
使用UIBezierPath类可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状 ...
- flask页面中Head标签内容为空问题
在使用flask时遇到点问题,以前还没有注意到. 生成页面的时候使用的是模板继承方式,当添加meta标题的时候,本来是添加的base.html模板中的head标签中,但是生成页面后,head中的内容却 ...
- vhdl基础---分频
偶数分频 ibrary IEEE; use IEEE.STD_LOGIC_1164.ALL; use ieee.std_logic_arith; use ieee.std_logic_unsigned ...
- 一、IRIG-B 概念
参考:http://baike.baidu.com/view/3601618.htm http://wenku.baidu.com/view/7956cd29bd64783e09122bf1.html ...
- curl库 c语言的curl 编程
c语言的curl 编程 [Linux@centos-64-min exercise]# gcc -Wall -o curltest curltest.c /tmp/ccosVANi.o: In fun ...
- How to insert a character into a NSString
How do I insert a space to a NSString. I need to add a space at index 5 into: NString * dir = @" ...
- 集成Spring后HibernateTemplate实现分页
spring 整合 hibernate 时候用的 HibernateTemplate 不支持分页,因此需要自己包装一个类进行分页,具体实现如下...使用spring的hibernateTemplate ...
- mysql几个命令
1.格式化输出 select * from mysql.user\G 2.显示版本 show version() 3.显示引擎 show engines mysql> show engines; ...
- php中curl的详细解说(转载)
本文转自:http://blog.csdn.net/yanhui_wei/article/details/21530811 这几天在帮一些同学处理问题的时候,突然发现这些同学是使用file_get_c ...
- 老陈 WPF
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...