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 guess which number I picked.
我从1到n之间选择一个数字,由你来猜我选了哪个数字。
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
):
你可以用一个已经准备好的接口guess(int num),它会返回3个可能的结果(-1,1或0):
-1 : My number is lower //我的数字更小
1 : My number is higher //我的数字更大
0 : Congrats! You got it! //猜中了
刚开始第一次提交采用了二分查找,不过显示超时了。
超时case: 2126753390
1702766719
看讨论发现是溢出了,所以改了下程序。
// 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 low=,high=n,g,r;
while(low<=high){
g=(low+high)/; //改成g=low+(high-low)/2;
r=guess(g);
if(r==-)high=g-;
else if(r==)low=g+;
else return g;
}
return ;
}
};
374. Guess Number Higher or Lower的更多相关文章
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
- leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II
374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...
- [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. 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. Guess Number Higher or Lower
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 We are ...
- 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 ...
- Python [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 t ...
- 【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode❤python】 374. Guess Number Higher or Lower
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...
随机推荐
- uml 在需求分析阶段的应用
上一篇博客写了uml在软件开发过程中的应用,这以篇要详细介绍一下UML在需求分析过程中的应用. 以机房收费系统为例进行讲解,先介绍一个该系统. 首先该系统的用户分为三个等级,一般用户,操作员,管理员, ...
- PowerDesigner 企业架构模型 ( EAM )
PowerDesigner 企业架构模型 ( EAM ) 说明 file工作数据库框架application网络 目录(?)[+] 一. 企业架构模型 说明 EnterpriseArchite ...
- ssh免密码登录记录
做mha.hadoop安装过程中都要用ssh免密码登陆,查过一些资料,踩过很多坑,下面用简单记录一下 首先要安装ssh linux : centOS 6.5 yum -y install *ssh* ...
- EF中Database.SqlQuery
本文转载:http://www.cnblogs.com/daimage/archive/2012/07/04/2575844.html EF中Database.SqlQuery<TElement ...
- CAS总结之Ticket篇
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Project Management - 3) Manage Your Meetings
1. 取消没有价值的会议 会议是有代价和成本的 不要举行顺序式的多人进度报告会议 eg: 这周做了什么,下周还要做什么? 除了发言人和项目经理外,每个人都会觉得无聊. 这种会议是在拖项目的后腿,赶紧停 ...
- unity3D中协程和线程混合
这是我google unity3D一个问题偶然发现的在stackflow上非常有趣的帖子: 大意是 要在unity3D上从server下载一个zip,并解压到持久化地址.并将其载入到内存中.以下展示了 ...
- python无私有成员变量
python解释器将__init__函数里的__z变量转成 _classname__z了,明确规则后外部依旧能够通过实力对象来訪问. In [1]: class aa: ...: def __init ...
- UIWebView的使用,简单浏览器的实现
#import "ViewController.h" @interface ViewController () <UIWebViewDelegate> @propert ...
- 使用RecyclerView实现瀑布流的效果
主函数: public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; privat ...