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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
分析
题目要求在产品的n个版本中找到第一个bad version。在所有版本中,若是当前i版本是bad,则后续i+1版本也是bad。
考察二分查找的应用。
AC代码
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
if (n < 1)
return -1;
int lhs = 1, rhs = n;
while (lhs < rhs)
{
int mid = lhs + (rhs - lhs) / 2;
//找到bad version
if (isBadVersion(mid))
{
rhs = mid;
}
//mid并不是bad version 则first bad肯定在右边
else{
lhs = mid + 1;
}
}//while
return lhs;
}
};
LeetCode(278)First Bad Version的更多相关文章
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- 洛谷-P3927 SAC E#1 - 一道中档题 Factorial
原址 题目背景 数据已修改 SOL君(炉石主播)和SOL菌(完美信息教室讲师)是好朋友. 题目描述 SOL君很喜欢阶乘.而SOL菌很喜欢研究进制. 这一天,SOL君跟SOL菌炫技,随口算出了n的阶乘. ...
- Collections集合工具类,集合嵌套,集合综合案例斗地主
1 Collections集合工具类 (可以对比Arrays工具类共同记忆) 常用方法: 例: import java.util.ArrayList; import java.util.Collect ...
- jsp-简单的猜数小游戏
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- The first step in solving any problem is recognizing there is one.
The first step in solving any problem is recognizing there is one.解决问题的第一步是要承认确实存在问题.
- Linux shell标准输入,标准输出,错误输出
shell中可能经常能看到:1>/dev/null 2>&1 eg:sudo kill -9 `ps -elf |grep -v grep|grep $1|awk '{prin ...
- UVA 624 CD(01背包,要记录路径)
题意: 有n张CD(n<=20),每张能播放的时长不同.给定一个时长限制t,挑出部分的CD使得总播放时间最长.顺便输出路径! 思路: 重点在输出路径,否则这题很普通.那就要用二维数组记录每个CD ...
- Java 基础案例
1.变量及基本数据类型 案例1:变量声明及赋值 //1.变量的声明 int a; //声明一个整型的变量a int b,c,d; //声明三个整型变量b,c,d //2.变量的初始化 int a = ...
- 【转】学习apicloud和IOS之间的模块化使用
最近公司有使用APICloud发开的需求,需要我这边提供一些模块包得封装.因为没有也是刚接触APICloud,所以也就在看官方文档 .下面讲一讲我再使用过程中得一点点东西. 首先,下载官方SDK,下载 ...
- CentOS配置主机名和主机映射
1.修改本机主机名 vi /etc/sysconfig/network 修改hostname HOSTNAME=s0 2.配置主机映射 vi /etc/hosts 修改内容如下 192.168.32. ...
- CentOS更改时区
1.编辑文件 vi /etc/sysconfig/clock 修改内容 ZONE="Asia/Shanghai" 2.覆盖旧时区文件 cp /usr/share/zoneinfo/ ...