【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 ...
随机推荐
- Vue—组件传值及vuex的使用
一.父子组件之间的传值 1.父组件向子组件传值: 子组件在props中创建一个属性,用以接收父组件传来的值 父组件中注册子组件 在子组件标签中添加子组件props中创建的属性 把需要传给子组件的值赋给 ...
- pycharm .sqlite文件拖动到Database里面为空
pycharm .sqlite文件拖动到Database里面为空 查资料得到解决方法:
- sql是最成功的第四代语言
SQL发展的前世今生 很多年前,两名年轻的IBM研究员将一门关系型语言带到了数据库领域,旨在使用声明性的方式来操作数据.从Don Chamberlin和Ramond Boyce发表"SEQU ...
- MongoDB 4.0.* 远程连接及用户名密码认证登陆配置——windows
- Tomcat热部署--start tomcat后就可自动部署war包
使用tomcat图形化界面,需要现在配置文件中设置用户名和密码: 在maven中配置Tomcat插件: root目录下的内容可以直接访问: 跳过测试: 查看端口占用:
- LODOP中tfoot和tbody中间线连不起来
这种情况发生在使用ADD_PRINT_TABLE时,ADD_PRINT_TABLE是Lodop中专门用来输出table表格的语句,它有很多特点,比如该语句不切行(详细可参考查看本博客相关博文:LODO ...
- 一道php笔试题
原文地址: http://www.walu.cc/php/a-bishiti.md 问题: 请找出下面代码中的问题,修复并优化. <?php //批量注册用户,每次>100个. //注册新 ...
- POJ2975 Nim 【博弈论】
DescriptionNim is a 2-player game featuring several piles of stones. Players alternate turns, and on ...
- [BJOI2017]树的难题
题目描述 给你一棵 n 个点的无根树. 树上的每条边具有颜色.一共有 m 种颜色,编号为 1 到 m.第 i 种颜色的权值为 ci. 对于一条树上的简单路径,路径上经过的所有边按顺序组成一个颜色序列, ...
- pre的内容自动转行
使pre的内容自动换行(转) <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见 ...