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.

思路也是典型的Binary Search, 只是需要注意的是guess(num) 每次对应的是猜的数高了, 还是答案高了即可.

Code
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
# def guess(num): class Solution(object):
def guessNumber(self, n):
l, r = 1, n
while l + 1 < r:
mid = l + (r-l)//2
num = guess(mid)
if num == 1:
l = mid
elif num == -1:
r = mid
else:
return mid
return l if guess(l) == 0 else r

[LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search的更多相关文章

  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] 278. First Bad Version_Easy tag: Binary Search

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  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] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  9. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

随机推荐

  1. Jenkins Docker安装及Docker build step插件部署配置

    生产部署环境:A:192.168.1.2 B:192.168.1.3  两台服务器系统均是Centos 7.3 , Docker版本都1.12.6 Jenkins安装操作步骤: 1.在A服务器上使用命 ...

  2. discuz 忘记安全密码的处理方式 修改pre_common_setting表的数据,

    一定要记住不要清空pre_common_setting表的数据,会给你带来烦恼的!!!!!只修改siteuniqueid 这条数据的密码就行!!!! 也可以先用我这条密码系统自动加密后:[DXRGYC ...

  3. Docker之OVS网络

    OVS介绍 什么是OpenVSwich? OpenvSwich:开放虚拟交换标准,是一种基于开源Apache2.0许可证的多层软件交换机,专门管理多租赁云计算网络环境,支持KVM.Xen等虚拟化技术. ...

  4. MQTT 单片机端讲解

    有空了和大家分享一下,如何从头架构一个高效mqtt并行客户端,基于传统GPRS等较差网络环境和网关等网络环境好的情景(当然仔细讲解mqtt的基本函数使很有必要的).---这会正忙着搬砖 MQTt协议 ...

  5. JavaScript中的Array类型详解

    与其他语言中的数组的区别: 1.JavaScript数组的每一项都可以保存任何类型的数据,一个数组的数组元素可以是不同类型的数据. 2.数组的大小是动态调整的,可以随着数据的添加自动的增长. 1.两种 ...

  6. ZOJ - 2853 Evolution 线性变换变成矩阵快速幂

    题意:给你N个数,1~N分别为num[i],  以及T个 (i,j,P) 对于每组(i,j,P),让你将  num[i] 减去 P*num[i]  再把 P*num[i] 加到 num[j] 上.T个 ...

  7. ppt转成图片等其它对象

    https://www.cnblogs.com/Yesi/p/5388152.html 从E-iceblue官网下载Free Spire.Presentation使用.下载完成后,请将bin文件夹的. ...

  8. [math][mathematica] Mathematica进阶

    1. Mathematica 画函数图像 2. Mathematica 解方程 见截图,敲完一行按Shift+Enter就可以执行了.主要函数名都是大小写敏感的.写的正确会跟有提示下拉框和相信说明,非 ...

  9. 按键控制led驱动

    内核版本:linux2.6.22.6 硬件平台:JZ2440 驱动源码 key_drv.c : #include<linux/module.h> #include<linux/ker ...

  10. 【SQL】where 后不可以接聚合函数,都哪些是聚合函数?

    where 后不可以接聚合函数,比如函数:SUM(count),AVG(count),MIN(count),MAX(count)