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 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个版本号[1, 2, …, n],如今你须要找出第一个损坏的版本号,它导致全部后面的版本号都坏掉了。
提供给你一个API bool isBadVersion(version)
。其功能是返回某一个版本号是否损坏。实现一个函数找出第一个损坏的版本号。你应该最小化调用API的次数。
原来仅仅需实现相似于Search for a Range 的功能,推断坏版本号的函数bool isBadVersion(version)
题目已经提供,无需纠结怎样操作,这里还是使用二分查找来解决这个问题。
三. 演示样例代码
期初使用例如以下代码一直提示:Time Limit Exceeded
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int low = 1, high = n;
int midIndex = 0;
while (low <= high)
{
midIndex = (low + high) / 2;
if (isBadVersion(midIndex))
high = midIndex - 1;
else
low = midIndex + 1;
}
return low;
}
};
检查了一段时间,发现当n取非常大的数时出现故障,原来是语句midIndex = (low + high) / 2;
直接相加时溢出。导致循环超时,该用下面代码Accept。
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int low = 1, high = n;
int midIndex = 0;
while (low <= high)
{
midIndex = low + (high - low) / 2;
if (isBadVersion(midIndex))
high = midIndex - 1;
else
low = midIndex + 1;
}
return low;
}
};
四. 小结
该题的难度在Search for a Range 之下。但出现了数据溢出的问题,因此对于简单一些的题也时刻不能掉以轻心啊。
leetcode笔记:First Bad Version的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
随机推荐
- 【SpringBoot】关闭HttpClient无用日志
环境: SpringBoot pom依赖了apache.commons.HttpClient: <!--httpclient--> <dependency> <group ...
- hdu 1114 dp动规 Piggy-Bank
Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- Hibernate 基于外键的单项一对一关联映射
在开发过程中很多时候会用到表与表之间一对一的关联关系,本文简单介绍在Hibernate4中单项一对一的关联映射. 1.设计表结构 2.创建Person对象 3.创建IdCard对象 4.写hbm.xm ...
- 【BZOJ 3136】 3136: [Baltic2013]brunhilda (数论?)
3136: [Baltic2013]brunhilda Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 238 Solved: 73[Submit][ ...
- [BZOJ1559][JSOI2009]密码(AC自动机)
http://www.lydsy.com/JudgeOnline/problem.php?id=1559 2009年的省选题虽然比起现在简单了不少,但对我来说还是很有挑战性的. 首先对于这种多串匹配问 ...
- org.apache.curator:master选举和分布式锁
1. master选举(LeaderSelector) 1)LeaderSelector构造函数 在leaderPath上建立分布式锁:mutex = new InterProcessMutex(cl ...
- 02-MariaDB主从安装SpringBoot整合MyBatis配置
关于MariaDB的介绍 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为My ...
- 今天测试了一下 sqlalchemy 性能
self.db.query(Users).filter(Users.Id==1).first() < self.db.execute('SELECT * FROM `users` WHERE ...
- wampserver -- 解决Exception Exception in module wampmanager.exe at 000F15A0
Learn from: http://hi.baidu.com/spt_form/item/4b4533476c3b92a6de2a9f78 系统:windows2003 32bit wampserv ...
- python开发_fileinput
python中,fileinput模块对读取文件操作提供了一些有用的方法 下面是我做的demo: 运行效果: ====================================== 代码部分: ...