【easy】278. First Bad Version
有一系列产品,从某个开始其后都不合格,给定一个判断是否合格的函数,找出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的更多相关文章
- 【leetcode】278. First Bad Version
problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...
- 【LeetCode】278. First Bad Version 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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 ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 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 ...
- 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 ...
随机推荐
- 【keras】用tensorboard监视CNN每一层的输出
from keras.models import Sequential from keras.layers import Dense, Dropout from keras.layers import ...
- 3 Eclipse 查看不了源码
Eclipse 查看源码时,报source not found问题,经查资料,需要配置环境变量才能查看到. 在用户变量或者系统变量下,配置CLASSPATH,值为JDK的lib路径:D:\Progra ...
- ASP.net中用到的JWT
1.先通过NuGet添加JWT 2.新建一个JwtHelp类 public class JwtHelp { //私钥 web.config中配置 //"GQDstcKsx0NHjPOuXOY ...
- Python——pyqt5——各框架编程
一.日期时间(dateTimeEdit/dateEdit) setDateTime:设置日期(含时间) QDateTime.currentDateTime():当前日期(含时间) setDate:设置 ...
- rabbtimq非持久化测试
send端代码 import pika,time,threading class send(): def __init__(self,que_nam='hello'): self.credential ...
- flex布局实例demo全解
上篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇> ...
- linux shell通配符及if语句判断
$# 是传给脚本的参数个数 $0 是脚本本身的名字$1 是传递给该shell脚本的第一个参数$2 是传递给该shell脚本的第二个参数$@ 是传给脚本的所有参数的列表$* 是以一个单字符串显示所有向脚 ...
- practice01
1. 组合数公式: C(n, k) =C(n-1, k) +C(n-1, k-1) 要求利用该公式写递归函数求组合数. #include <stdio.h> int C(int a,int ...
- 集合源码分析[1]-Collection 源码分析
目录 Collection 1. 介绍 2. 继承关系 3. 方法 4. JDK8新增的方法 removeIf(Predicate<? super E> filter) Spliterat ...
- 一加3T 误清除data 恢复数据
数据丢失经过:日常用机无备份直接操作:装google框架后,rootexplorer文件浏览器删除多余google应用导致无法开机:开机不成功应该重刷入google gapps包,并没有这样操作而是进 ...