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 ...
随机推荐
- 133个Java面试问题列表
转载: 133个Java面试问题列表 Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别就能让你直接进入第二轮面试,但是现在问题变得越来 ...
- 【C#】利用反射构建实体
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- VideoView视频缓冲进度条
效果图: 需求: 刚进入视频播放页时,屏幕中间有加载进度条 视频播放过程中,视频界面不动了,正在缓冲时,屏幕中间有加载进度条 private ObjectAnimator rotate; ImageV ...
- Java线程池使用和常用参数(待续)
线程池怎么实现的,核心参数讲一讲? Executors是线程池的工厂类,通过调用它的静态方法如下: Executors.newCachedThreadPool(); Executors.newFixe ...
- Tornado(一)
Tornado 特点 Tornado是一个用Python写的相对简单的.不设障碍的Web服务器架构,用以处理上万的同时的连接口,让实时的Web服务通畅起来.虽然跟现在的一些用Python写的Web架构 ...
- python统计文本中每个单词出现的次数
.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...
- 山东省第四届ACM程序设计竞赛A题:Rescue The Princess
Description Several days ago, a beast caught a beautiful princess and the princess was put in prison ...
- hdu 1171 多重背包
题意:给出价值和数量,求能分开的最近的两个总价值,例如10,20*2,30,分开就是40,40 链接:点我 #include<cstdio> #include<iostream> ...
- ZOJ 3707 Calculate Prime S 数论
思路:容易得到s[n]=s[n-1]+s[n-2],也就是fib数. 求第k小的fib质数的也就是第k个质数数-2,当k>2时. 在就是s[n]/x%m=s[n]%(x*m)/x. 代码如下: ...
- bzoj 2763: [JLOI2011]飞行路线 -- 分层图最短路
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MB Description Alice和Bob现在要乘飞机旅行,他们选择了一家相 ...