[LeetCode] Guess Number Higher or Lower 猜数字大小
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I'll tell you whether the number is higher or lower.
You call a pre-defined API guess(int num)
which returns 3 possible results (-1
, 1
, or 0
):
-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!
Example:
n = 10, I pick 6. Return 6.
这道题是一道典型的猜价格的问题,根据对方说高了还是低了来缩小范围,虽然是道 Easy 题,无脑线性遍历还是会超时 Time Limit Exceeded,所以更快速的方法就是折半搜索法,原理很简单,属于博主之前的总结帖 LeetCode Binary Search Summary 二分搜索法小结 中的第四类-用子函数当作判断关系,参见代码如下:
int guess(int num); class Solution {
public:
int guessNumber(int n) {
if (guess(n) == ) return n;
int left = , right = n;
while (left < right) {
int mid = left + (right - left) / , t = guess(mid);
if (t == ) return mid;
if (t == ) left = mid + ;
else right = mid;
}
return left;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/374
类似题目:
Guess Number Higher or Lower II
参考资料:
https://leetcode.com/problems/guess-number-higher-or-lower/
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/84664/0ms-c%2B%2B-binary-search
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Guess Number Higher or Lower 猜数字大小的更多相关文章
- [LeetCode] 374. Guess Number Higher or Lower 猜数字大小
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- 374 Guess Number Higher or Lower 猜数字大小
我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int n ...
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Leetcode: Guess Number Higher or Lower II
e are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess ...
- Leetcode: Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小 II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
- [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
随机推荐
- OCP考点实战演练01-备份恢复篇
本系列宗旨:真正掌握OCP考试中所考察的技能,坚决不做Paper OCP! 实验环境:RHEL 6.4 + Oracle 11.2.0.4 OCP考点实战演练01-备份恢复篇 1.数据库开启归档 2. ...
- 【转】基于linux下的dm9000网卡移植全分析
转自:http://blog.sina.com.cn/s/blog_6abf2c04010189ui.html DM9000可以直接与ISA总线相连,也可以与大多数CPU直接相连.Mini2440采用 ...
- internet协议入门
前言 劳于读书,逸于作文. 原文地址:internet协议入门 博主博客地址:Damonare的个人博客 博主之前写过一篇博客:网络协议分析,在这篇博客里通过抓包,具体的分析了不同网络协议的传送的数据 ...
- reStructuredText(rst)快速入门语法说明
reStructuredText 是扩展名为.rst的纯文本文件,含义为"重新构建的文本"",也被简称为:RST或reST:是Python编程语言的Docutils项目的 ...
- The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory 这是由于项目里面的一些 ...
- AOP的实现原理
1 AOP各种的实现 AOP就是面向切面编程,我们可以从几个层面来实现AOP. 在编译器修改源代码,在运行期字节码加载前修改字节码或字节码加载后动态创建代理类的字节码,以下是各种实现机制的比较. 类别 ...
- JAVA JSP笔记
一.jsp加载项目中资源图片 如果直接将静态页面写的代码copy到jsp中,你会发现图片都无法加载. 获取代码: String path = request.getContextPath(); Str ...
- Cobar + MySQL 技术验证(li)
一.简介 Cobar是一个对数据进行拆分后进行分布式存储的产品,可以支持使用后台的 MySQL或者Oracle数据库,通过配置,将数据按照一定规则存储入不同的数据库中.即用分布式数据库代替了集中式数据 ...
- ArcGIS Engine开发之量测功能
1.距离测量 距离测量时,片段长度通过两点之间距离计算得到,全部长度通过片段长度的和计算得到.主要用到INewLineFeedback和IScreenDisplay两个接口. 1)INewLineFe ...
- UITabBarController 升级定制
UITabBarController 定制 特点 用法 1.准备工作: 加入你的相关图片,放入了Assets.xcassets; 导入Categroy文件夹(这个里面的文件,在这里不详细说明了,有疑问 ...