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.

这道题是一道典型的猜价格的问题,根据对方说高了还是低了来缩小范围,虽然是道 Easy 题,无脑线性遍历还是会超时 Time Limit Exceeded,所以更快速的方法就是折半搜索法,原理很简单,属于博主之前的总结帖 LeetCode Binary Search Summary 二分搜索法小结 中的第四类-用子函数当作判断关系,参见代码如下:

int guess(int num);

class Solution {
public:
int guessNumber(int n) {
if (guess(n) == ) return n;
int left = , right = n;
while (left < right) {
int mid = left + (right - left) / , t = guess(mid);
if (t == ) return mid;
if (t == ) left = mid + ;
else right = mid;
}
return left;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/374

类似题目:

Guess Number Higher or Lower II

First Bad Version

Find K Closest Elements

参考资料:

https://leetcode.com/problems/guess-number-higher-or-lower/

https://leetcode.com/problems/guess-number-higher-or-lower/discuss/84664/0ms-c%2B%2B-binary-search

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 374. Guess Number Higher or Lower 猜数字大小的更多相关文章

  1. 374 Guess Number Higher or Lower 猜数字大小

    我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int n ...

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

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

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

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

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

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

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

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

随机推荐

  1. 聊一下,前后分离后带来的跨域访问和cookie问题

    在谈前后分离前,我们先看看什么是前后一体的.当我们用javaweb开发网站时,最终我们渲染的jsp或者springthymeleaf.我们的页面其实是WEB-INFO或者templates下.当用户请 ...

  2. Oracle中TIMESTAMP时间的显示格式

    Oracle中的TIMESTAMP数据类型很多人用的都很少,所以即使最简单的一个查询返回的结果也会搞不清楚到底这个时间是什么时间点. 例如: 27-1月 -08 12.04.35.877000 上午 ...

  3. element-ui的tabs默认选中页签

    Element-UI提供了tabs组件(选项卡.多页签),其中在tabs的属性中提供了一个value/v-model属性来绑定默认选中的页签. 我们通过简单的示例来看一下具体是怎么使用的. <t ...

  4. 超详细Pycharm部署项目视频教程

    在实际的工作中,不管你是开发.测试还是运维人员,都应该掌握的一项技能就是部署项目,简单说就是把项目放到服务器中,使其正常运行.今天猪哥就以咱们的微信机器人项目为例子,带大家来部署一下项目.本文将会详细 ...

  5. 阿里云(百度云)Linux系统挂载磁盘

    阿里云(百度云)Linux系统挂载磁盘

  6. 简单ALV得演示(用到了ALV可编辑及保存后修改数据库)

    *&---------------------------------------------------------------------* *& Report YPMRP010_ ...

  7. 三维网格精简算法(Quadric Error Metrics)附源码(转载)

    转载:  https://www.cnblogs.com/shushen/p/5311828.html 在计算机图形应用中,为了尽可能真实呈现虚拟物体,往往需要高精度的三维模型.然而,模型的复杂性直接 ...

  8. Linux open fopen fdopen

    int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); 以 ...

  9. 【设计模式】Builder

    前言 Builder设计模式,允许一步一步构建一个复杂的对象.将构建步骤抽象出来,让每个具体的Builder去实现构建步骤的内容.这样子就可以用同样的构建步骤,构建出不一样的对象.在Director类 ...

  10. 章节十四、5- web页面的截图

    一.以雅虎网站为例,当我们在登录时,输入错误的用户名然后点击“下一步”,用户名输入框会提示红色字体,这个时候我们就将页面进行截图. http://commons.apache.org/proper/c ...