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. nodejs XML和json互相转换

    Docs: https://www.npmjs.com/package/fast-xml-parser const xml = ` <user> <name>ajanuw< ...

  2. Thrift版本管理

    对于健壮的服务,其中的数据类型必须一种机制来对其进行版本管理,尤其是它可以在不中断服务(或者更坏的情况,出现段错误)的前提下,增加或删除一个对象中的字段,或者改变一个函数的参数列表. 字段标识符 Th ...

  3. Nordic NRF51822 从零开始系列(一)开发环境的搭建

    硬件准备     (1)nrf51822 开发板一块(此处使用的是青云系列的,自带jlijnk ob+usb串口芯片)或者使用nrf51822模块+jlink_ob                 ( ...

  4. poj1734 Sightseeing trip【最小环】

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:8588   Accepted:3224   ...

  5. 电子产品使用感受之--Windows 10 小米笔记本Air HDMI转VGA无信号问题

    最近一直通过HDMI转VGA线缆链接我的戴尔P2314H显示器,前天睡觉前,看到电脑上英伟达显卡推了驱动更新,顺手更新了一下,就去睡觉了,转天晚上再用,HDMI接口就没有信号了,上网查了一些信息,获知 ...

  6. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  7. Lombok安装及使用

    为什么要使用Lombok: 虽然一般的IDE(eclipse,Intellij)都有自动生成代码的功能,如:生成setter,getter,toString,equels,hashcode等.但是如果 ...

  8. webpack.DefinePlugin

    通过配置了DefinePlugin,那么这里面的标识就相当于全局变量,你的业务代码可以直接使用配置的标识.比如,你通过下面的设置: // webpack.config.js new webpack.D ...

  9. piano class 13

    1,手放在琴键上,不妨碍另外一只手弹奏即可 2,识别五线谱可以加上几条线,减去几条线,一下子就记住了所有的 3,弹得还是有点快,要慢弹奏,四四拍的理论上比四三拍的还要慢,也要看风格 4,四二拍,强弱, ...

  10. Sharding与数据库分区(Partition) 分表、分库、分片和分区

    Sharding与数据库分区(Partition) http://blog.sina.com.cn/s/blog_72ef7bea0101cjtb.html https://www.2cto.com/ ...