一. 题目描写叙述

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的更多相关文章

  1. 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 ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  5. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  10. 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 ...

随机推荐

  1. 微信用户授权,获取code

    1.进入微信公众平台 2.进入到   开发->接口授权,点击 网页服务->网页授权->网页授权获取用户基本信息   后面的“修改“. 3.点击网页授权域名的设置 4.设置授权回调域名 ...

  2. Wordpress,你好!

    [caption id="" align="alignleft" width="1024"] 耳机[/caption] 想了想,还是没有删掉 ...

  3. 【BZOJ 3242】 (环套树、线段树+树形DP?)

    3242: [Noi2013]快餐店 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 728  Solved: 390 Description 小T打算 ...

  4. android fragment activity 区别

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha fragment  负责一个模块 的展示. 由 活动 管理. 碎片 可以 解决 太多活动 ...

  5. 2017-2018-1 JAVA实验站 冲刺 day05

    2017-2018-1 JAVA实验站 冲刺 day05 各个成员今日完成的任务 小组成员 今日工作 完成进度 张韵琪 进行工作总结 100% 齐力锋 找按钮音乐 100% 张浩林 写博客 100% ...

  6. 1.4(JavaScript学习笔记) window对象的属性及方法

    一.window对象 window对象代表当前窗口,所有全局对象都是windows的属性, 例如document是window的属性,window.document.writer("&quo ...

  7. Codeforces Beta Round #10 D. LCIS 动态规划

    D. LCIS 题目连接: http://www.codeforces.com/contest/10/problem/D Description This problem differs from o ...

  8. 比较page、request、session、application的使用范围(转自用)

    (1)直接在web contain中进行对象的实例化. 内置对象 类型 作用域 pageContext javax.servlet.jsp.pageContext page request javax ...

  9. 使用Keras开发神经网络

    一.使用pip安装好tensorflow 二.使用pip安装好Keras 三.构建过程: 1 导入数据 2 定义模型 3 编译模型 4 训练模型 5 测试模型 6 写出程序 1.导入数据 使用皮马人糖 ...

  10. Extjs window组件 拖动统制

    Extjs window组件 拖动控制有时候一拖就拖出了浏览器,在想拖回来就不好办了: 解决办法:参考以下代码,在加载Ext核心库以后执行: Ext.override(Ext.Window, {    ...