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的更多相关文章

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

    ProxyFactory 是 Spring AOP的实现方式之一.下面介绍下ProxyFactory的用法. 1.接口定义 public interface UserReadService { pub ...

  2. mysql视图和存储过程定义者修改脚本(懒人专用)

    前言: 在实际工作中mysql数据库的迁移.备份恢复.数据库重命名等一系列涉及到视图和存储过程定义者问题都会需要修改,每次都要从基础表获取数据,然后手工整理做脚本,十分麻烦,所以简单写了个过程,以后可 ...

  3. iOS 有关自动轮播图片

    //初始化当前视图 _currentImageView = [[UIImageView alloc] init]; [_currentImageView setImageWithURL:[NSURL ...

  4. TortoiseGit disconnected: no supported authentication methods available(server sent:publickey)

    之前一直用命令行,现在想用图形工具,TortoiseGit,安装后遇到错误 TortoiseGit disconnected: no supported authentication methods ...

  5. PHP PDO函数库具体解释

    文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/565 PDO是一个"数据库訪问抽象层",作用是统一各种数据库的訪问接口 ...

  6. 【BZOJ2318】【spoj4060】game with probability Problem 概率DP

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  7. careercup-C和C++ 13.3

    13.3 C++中的虚函数是如何工作的? 解答 虚函数依赖虚函数表进行工作.如果一个类中,有函数被关键词virtual进行修饰, 那么一个虚函数表就会被构建起来保存这个类中虚函数的地址.同时, 编译器 ...

  8. 内网架设SVN服务器相关设置

    环境说明:        外网IP为静态IP,工作室内有多台电脑,公用一台路由器.想要在某台电脑上架设SVN服务器,方便团队开. 解决方案: 一.准备工作1.获取 Subversion 服务器程序到官 ...

  9. Android(java)学习笔记125:Clock app编写报错02

    1.首先之间看错误: 07-13 10:07:55.354: E/AndroidRuntime(8008): FATAL EXCEPTION: main 07-13 10:07:55.354: E/A ...

  10. mvn打包

    mvn install -DskipTests mvn install -Dmaven.test.skip=true