有一系列产品,从某个开始其后都不合格,给定一个判断是否合格的函数,找出N个产品中第一个不合格的产品。

正确答案:

// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int low = ;
int high = n;
int ver = ;
while (low < high) {
ver = low + (high - low) / ;
//这个题注意点!!如果直接high+low可能溢出,就会超时…… if (isBadVersion(ver)) {
high = ver;
} else {
low = ver + ;
}
}
return low;
}
};

=============

有一道非常类似的题目

猜数游戏:1-n之内有一个数是预选的数字,每次回告诉大还是小或者正确。返回猜中的数字。

// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num); class Solution {
public:
int guessNumber(int n) {
int start = ;
int end = n;
int res = ;
while (start<end){
int mid = start + (end-start)/;
if (guess(mid)==){
return mid;
}
if (guess(mid)==)
start = mid+;
else
end = mid-;
}
if (guess(start))
return start;
return end;
}
};

【easy】278. First Bad Version的更多相关文章

  1. 【leetcode】278. First Bad Version

    problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...

  2. 【LeetCode】278. First Bad Version 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.c ...

  3. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  4. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  5. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  8. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  9. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

随机推荐

  1. C#需要在项目程序生成前后执行相关的事件

    分享4: 需求:需要在项目程序生成前后执行相关的事件,比如:需要将某个文件拷贝到bin\Debug中,或者创建某文件夹等. 分析:我们可利用项目属性(选择项目右键,选择属性)中的“生成事件”预定义相关 ...

  2. C#之重写与隐藏

    一 重写与隐藏区别 (1)方法重写:就是在基类中的方法用virtual关键字来标识,然后在继承类中对该类进行重写(override),这样基类中的方法已经被重写了,已经失去了功能了.当让基类的对象的引 ...

  3. js函数库-D3

    推荐: https://www.cnblogs.com/createGod/p/6884629.html

  4. 【MongoDB异常】Exception authenticating MongoCredential解决方法

    我们通过ideal编辑器编辑 springboot时候,出现这个错误: com.mongodb.MongoSecurityException: Exception authenticating Mon ...

  5. spring cloud配置注册中心显示服务的ip地址和端口

    1.在springcloud中服务的 Instance ID 默认值是: ${spring.cloud.client.hostname}:${spring.application.name}:${sp ...

  6. [题解]邮递员寄信(luoguP1629)

    题目来源:luoguP1629 题目描述 有一个邮递员要送东西,邮局在结点1.他总共要送N-1样东西,其目的地分别是2-N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条 ...

  7. 【一本通1248:Dungeon Master&&洛谷UVA532 Dungeon Master】

    若不会广搜转向[广搜] [题目描述] 这题是一个三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了 ...

  8. PageRank算法初探

    1. PageRank的由来和发展历史 0x1:源自搜索引擎的需求 Google早已成为全球最成功的互联网搜索引擎,在Google出现之前,曾出现过许多通用或专业领域搜索引擎.Google最终能击败所 ...

  9. EF Code First关系规则及配置

    1.一对多关系 关系表: Category 分类表 Product 产品表 分类与产品之间的一对多关系 1>.产品实体类不指定外键属性 Domain中类定义: Category.cs 1 usi ...

  10. Form 表单相关小技巧

    JS ---textarea 高度自适应 var realH = this.scrollHeight + 10+ "px"; $(this).css("height&qu ...