【leetcode73】经典算法-Guess Number Higher or Lower
题目描述:
从1~n中,随便的拿出一个数字,你来猜测。
提示
提供一个guess(int num)的api,针对猜测的数字,返回三个数值。0,-1,1
- 0;猜中返回num
- -1:比猜测的数值小
- 1:比猜测的数值大
例如:
n = 10, I pick 6.
Return 6.
原文描述:
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.
思路分析:
- 明显是二分法的应用,关于二分法,看我之前的一片博客,讲的比较详细,链接如下:
Java语言实现二分法
代码:
/* The guess API is defined in the parent class GuessGame.
@param num, your guess
@return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num); */
public class Solution extends GuessGame {
public int guessNumber(int n) {
int L = 1,R = n;
while(L <= R){
int mid = L + ((R - L) >> 1);
int res = guess(mid);
if(res == 0) return mid;
else if(res == 1) L = mid + 1;
else R = mid - 1;
}
return L;
}
}
更多leetcode题目,请看我的leetcode专栏。链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode73】经典算法-Guess Number Higher or Lower的更多相关文章
- [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 ...
- 374&375. Guess Number Higher or Lower 1&2
做leetcode的题 We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You ...
- [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 ...
- [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] 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
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- Guess Number Higher or Lower II--困惑
今天,试着做了一下LeetCode OJ上面的第375道题:Guess Number Higher or Lower II 原题链接:https://leetcode.com/problems/gue ...
- 每天一道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 ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
随机推荐
- 解决警告: Setting property 'source' to 'org.eclipse.jst.jee.server_:' did not find a matching property.的方法
今天第一次搭建struts2框架,跟着网上的教程导入对应的jar包之后就开始写登录的jsp页面,但是运行时出现了问题, 浏览器显示"The requested resource is not ...
- Windows环境下,从零开始搭建Nodejs+Express+Ejs框架(一)---安装nodejs
第一步,安装nodejs https://nodejs.org/en/download/ 这个是nodejs的官网,由于操作系统是win7 64位的,所以,我下载的是node-v8.11.1-x64的 ...
- Git 常用命令速查表
- B+树在磁盘存储中的应用
欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 我们首先提一个问题, B+树比平衡二叉树在索引数据方面要快么? 大多数人可能认为肯定还是 ...
- SimpleDateFormat中parse和format的区别
parse()返回的是一个Date类型数据,format返回的是一个StringBuffer类型的数据 //SimpleDateFormat中的parse方法可以 //把String型的字符串转换成特 ...
- SQL LOADER使用
转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17048907 1.执行的命令 sqlldr 数据库用户名/密码 control=控制文 ...
- @property的参数
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51353580 本文出自:[openXu的博客] 参数类别 参数 说明 原子性 atomic ...
- CSDN 支持Markdown写文章了!
开源中国等其他技术博客很早就支持markdown格式写文章了,今天发现csdn竟然也可以了,不仅支持而且可以在线预览,本地导入导出,远程导入. 这些对于程序员写东西都非常好用,不用总是花时间来排版了. ...
- nginx反向代理和rewrite进行解决跨域问题、去掉url中的一部分字符串,通过nginx正则生成新的url
场景:表面上访问的是http://127.0.0.1:7777/test/xhtml//tpl/app-tpl-webapp/css/base.css, 实际上看的是http://127.0.0.1: ...
- springmvc文件上传和拦截器
文件上传 用到这两个包 配置视图解析器:springmvc配置文件配置 <!-- id必须要是"multipartResolver" --> <bean id=& ...