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.

Binary Search

Here "My" means the number which is given for you to guess not the number you put into guess(int num).

 /* 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 m = l + (r-l)/2;
int guessRes = guess(m);
if (guessRes == 0) return m;
else if (guessRes == -1) r = m - 1;
else l = m + 1;
}
return Integer.MAX_VALUE;
}
}

Leetcode: Guess Number Higher or Lower的更多相关文章

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

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

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

  4. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

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

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

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

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

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

  9. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

随机推荐

  1. php 文件和表单内容一起上传

    <?php $filename = $_POST['filename']; $explain = $_POST['explain']; $upfile = $_FILES['upfile']; ...

  2. Converting from Decimal Notation to Binary Notation for Fractions

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Therefore, the conver ...

  3. ArcGIS Server发布服务,报错00032

    错误00032 独立表数据源无法访问 00032: 修复已断开的数据连接,具体做法是将每个独立表的数据源都设置为正确的位置. 或者,如果不需要该表,将其从文档中移除. 建议在ArcMap中重新加载数据 ...

  4. java面试问道的

    1.java可重入锁 2.Hashmap原理.说说hashMap是怎样实现的(这个之前看过,顺利回答上.还回答了多线程的问题出现的原因,面试官表示很惊讶的样.用hashmap实现hashset 3.

  5. java.lang.ClassNotFoundException: springosgi

    该问题困扰多天,终于查到原因. 问题:对webwork源码的修改始终无法加载,osgi总是读取源码中未修改的类 com.opensymphony.webwork.dispatcher.Dispatch ...

  6. 通过nginx配置文件抵御攻击

    通过nginx配置文件抵御攻击 囧思九千 · 2013/11/12 12:22 0x00 前言 大家好,我们是OpenCDN团队的Twwy.这次我们来讲讲如何通过简单的配置文件来实现nginx防御攻击 ...

  7. GIS 学习及参考站点

    地理信息论坛 GIS空间站 GISALL 广东水利厅 flex版的

  8. windows下安装yaf和git

    不得不说win7下安装yaf比mac下安装yaf简单多了 1. phpinof()看一下你的php版本.我的是php 5.4所以我选择是php_yaf-2.1.9-x86-5.4-zts-nodebu ...

  9. Mybatis在xml文件中处理大于号小于号的方法

    第一种方法:用了转义字符把">"和"<"替换掉,然后就没有问题了. AND start_date <= CURRENT_DATE AND en ...

  10. http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html

    http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html http://bbs.csdn.net/topics/380187593