题目描述:

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 (-11, 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.

要完成的函数:

int guess(int num);//api函数

int guessNumber(int n)

说明:

1、给定一个整数 n,从1到n中挑一个数出来,让你猜是哪个数。每次你猜一个数,调用一次api函数,返回0就代表刚好是这个数,你猜对了!如果返回-1,就表示你猜的数太大了,实际的数比这个小。如果返回1,就表示你猜的数太小了,实际的数比这个大。要求最终返回对的那个数。

2、这道题很明显是一道二分查找的题目,手工写一个二分查找的代码。

代码如下:(附详解)

    int guess(int num);
int guessNumber(int n)
{
int low=1,high=n,mid,t;
while(low<=high)
{
mid=low+(high-low)/2;//写成 mid=(low+high)/2 可能会上溢
t=guess(mid);
if(t==0)
return mid;
else if(t==-1)//实际的数比mid小
high=mid-1;
else //实际的数比mid大
low=mid+1;
}
}

上述代码实测2ms,beats 100.00% of cpp submissions。

leetcode-374-Guess Number Higher or Lower(二分查找)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. set 续3

    -------siwuxie095                 set 技巧高级篇:     1.利用 set /a 进行赋值     在开启变量延迟情况下,要判断数组 S!n! 的值的情况, 不 ...

  2. 单源最短路:Dijkstra算法 及 关于负权的讨论

    描述: 对于图(有向无向都适用),求某一点到其他任一点的最短路径(不能有负权边). 操作: 1. 初始化: 一个节点大小的数组dist[n] 源点的距离初始化为0,与源点直接相连的初始化为其权重,其他 ...

  3. 子串字谜substring anagrams

    [抄题]: 给定一个字符串 s 和一个 非空字符串 p ,找到在 s 中所有关于 p 的字谜的起始索引.字符串仅由小写英文字母组成,字符串 s 和 p 的长度不得大于 40,000.输出顺序无关紧要. ...

  4. 解决clion2016.3不能支持搜狗输入法的问题

    参考链接http://www.cnblogs.com/chentq/p/4975794.html 打开clion.sh在文件头部添加 export GTK_IM_MODULE=fcitx export ...

  5. JavaScript stringObject.replace() 方法

    定义和用法: replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法: stringObject.replace(RegExp/substr,reol ...

  6. PHP5.2 $arr = [] 初始化数组出现问题

    初始化数组  $arr=[] ,出现问题,使用 $arr = array() ,一切正常

  7. shell脚本生成xml文件

    今天把这段时间学习完shell后完成工作上的一个小案件整理了一下,分享给大家! 说来也巧了,作为一个刚刚毕业半年的菜鸟,进入公司后,听公司的大牛推荐学习linux--”鸟哥的私房菜“,基本上是从去年8 ...

  8. arean.c

    glibc-2.14中的arean.c源代码,供研究malloc和free实现使用: /* Malloc implementation for multiple threads without loc ...

  9. yndbtree控件

    yndbtree控件 // cxg 2017-4-25 unit yndbtree; interface uses SysUtils, Classes, ComCtrls, DB, Variants ...

  10. EAS_AOP分布式事务

    在System.Transactions事务体系中,为事务提供了7种不同的隔离级别.这7中隔离级别分别通过 System.Transactions.IsolationLevel的7个枚举项表示. pu ...