Java [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.
解题思路:
使用二分法查找。同时注意二分法查找时候中间位置的数值不能溢出。
代码如下:
/* 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 start = 1, end = n;
while(start < end){
int mid = start + (end - start) / 2;
if(isBadVersion(mid))
end = mid;
else
start = mid + 1;
}
return start;
}
}
Java [Leetcode 278]First Bad Version的更多相关文章
- [LeetCode] 278. 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
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- (medium)LeetCode 278.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 二分查找(二分下标)
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...
- [leetcode]278. 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(E)(P)
题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...
- 278. First Bad Version - LeetCode
Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- 【leetcode】278. First Bad Version
problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...
随机推荐
- JS实现Web网页打印功能(IE)
问题描述: JS实现Web网页打印功能 问题解决: 这里主要使用WebBrowser控件的ExeWB在IE中打印功能的实现 WebBrowser介绍: WebBrows ...
- HTML5网页制作教程:HTML5块级链接
网页制作Webjx文章简介:Web 标准中处处充满了打脸行为,这条规则现在已经失效了!在那篇文章发布一个月后,HTML5doctor 发表了 “Block-level” links in HTML5, ...
- ural 1864
题意描述不清 而且还卡精度 ~~ #include <cstdio> #include <cstring> #include <iostream> using ...
- Unity3D研究院之IOS全自动打包生成ipa
接着上一篇文章, 自动生成framework,这篇文章我把shell自动化打包ipa整理了一下,希望大家喜欢,嘿嘿.. 建议大家先看一下上一篇文章.http://www.xuanyusong.com/ ...
- winform窗口打开后文本框的默认焦点设置
原文:http://blog.csdn.net/kongwei521/article/details/6871411 winform窗口打开后文本框的默认焦点设置,进入窗口后默认聚焦到某个文本框,两种 ...
- ***常见复杂SQL语句(含统计类SQL)
1.SQL统计某字段的出现次数 比如统计某个表中,姓名出现的次数:select name,count(*) from biao group by name having count(*) > 2 ...
- httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数
一.利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单 package com.test.httpclient; imp ...
- lintcode: 中序遍历和后序遍历树构造二叉树
题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 / \ 1 3 注意 你可 ...
- 如何发布使用LGPL版Qt的商业软件
最近做跨平台图形用户界面库选型,权衡很多因素后最终选择了Qt,其中一个重要因素就是Qt使用LGPL授权许可.由于本人对LGPL理解有限,始终对闭源商业软件如何发布Qt库存在疑问,其中最关心的是:发布的 ...
- Qt学习记录--Qt::CaseSensitive
Qt::CaseSensitivity 为枚举类型, 可取值Qt::CaseSensitive 和 Qt::CaseInsensitive, 表示匹配的灵敏度. 比较字符串的时候 Qt::CaseSe ...