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. -1 : My number is lower    //我的数字更小
  2. 1 : My number is higher   //我的数字更大
  3. 0 : Congrats! You got it!  //猜中了

刚开始第一次提交采用了二分查找,不过显示超时了。

超时case: 2126753390

       1702766719

看讨论发现是溢出了,所以改了下程序。

  1. // Forward declaration of guess API.
  2. // @param num, your guess
  3. // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
  4. int guess(int num);
  5.  
  6. class Solution {
  7. public:
  8. int guessNumber(int n) {
  9. int low=,high=n,g,r;
  10. while(low<=high){
  11. g=(low+high)/;  //改成g=low+(high-low)/2;
  12. r=guess(g);
  13. if(r==-)high=g-;
  14. else if(r==)low=g+;
  15. else return g;
  16. }
  17. return ;
  18. }
  19. };
  1.  

374. Guess Number Higher or Lower的更多相关文章

  1. Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)

    Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 【一天一道LeetCode】#374. Guess Number Higher or Lower

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 We are ...

  6. 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 ...

  7. 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 ...

  8. 【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【leetcode❤python】 374. Guess Number Higher or Lower

    #-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...

随机推荐

  1. 【STL源码学习】STL算法学习之二

    第一章:前言 学习笔记,记录学习STL算法的一些个人所得,在以后想用的时候可以快速拾起. 第二章:明细 copy 函数原型: template <class InputIterator, cla ...

  2. Php AES加密、解密与Java互操作的问题

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  3. cocos2d-x Menu、MenuItem

    转自:http://codingnow.cn/cocos2d-x/832.html 学习cocos2d-x中的菜单主要需要了解:菜单(CCMenu)和菜单项(CCMenuItem)以及CCMenuIt ...

  4. Graph(2014辽宁ACM省赛)

    问题 F: Graph 时间限制: 1 Sec  内存限制: 128 MB 提交: 30  解决: 5 [cid=1073&pid=5&langmask=0" style=& ...

  5. android 删除文件以及递归删除文件夹

    private void deleteDirectory(File file) { if (file.isFile()) { file.delete(); return; } if(file.isDi ...

  6. UITableViewCell 高度自适应

    UITableViewCell 高度自适应一直是我们做动态Cell高度时遇到的最烦躁的问题,Cell动态高度计算可以去看看sunny的这篇文章介绍,今天主要和大家分享下我在使用systemLayout ...

  7. percona监控模板图形解释

    http://blog.itpub.net/28916011/viewspace-1971933/ percona监控mysql的几张图形解释     最近,我仔细研究了一下percona监控mysq ...

  8. Python源码剖析

    http://blog.csdn.net/balabalamerobert/article/details/570758

  9. Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色

    自从IOS7后UINavigationBar的一些属性的行为发生了变化.你可以在下图看到: 现在,如果你要修改它们的颜色,用下面的代码: self.navigationController.navig ...

  10. 将字符串写进txt中方式

    try { File file = new File(filePath); PrintStream ps = new PrintStream(new FileOutputStream(file)); ...