[LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search
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, 只是需要注意的是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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
- [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. ...
- 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 ...
随机推荐
- Android所有Demo资源汇总,太全了(申明:来源于网络)
Android所有Demo资源汇总,太全了(申明:来源于网络) 地址:http://bbs.csdn.net/topics/391928947
- db2 活动日志激增的原因分析处理
本文简单地介绍了DB2中日志的使用.活动日志以及首个活动日志的概念.日志满的原因.日志满的诊断.临时处理以及避免办法 日志使用 下图显示了并发事务条件下,日志使用的示意 有3个并发的程序Process ...
- Maven知识点积累一
配置maven变量,变量名可以是:MAVEN_HOME 或 M2_HOME settings.xml配置本地仓库地址: <localRepository>G:/.m2/repository ...
- 使用Kdenlive为视频加入马赛克特效
Kdenlive(KDE Non-Linear Video Editor)是一种基于MLT框架.KDE和Qt的自由开源的非线性影片编辑器.其底层包含了FFmpeg,所以可以支持FFmpeg中的所有视频 ...
- .NET Core开发日志——Action
在叙述Controller一文中,有一处未做解释,即CreateControllerFactory方法中ControllerActionDescriptor参数是如何产生的.这是因为其与Action的 ...
- 难以接受你的改变:从project.json到.csproj
自从微软做了一个艰难的决定——.NET Core彻底放弃project.json,全面改回.csproj——至今,虽然赞美之声不断,但我依然不喜欢也难以接受这样的改变. 难以接受主要有两方面的原因: ...
- ubuntu经常断网、掉线、上不去网的原因
方案一: ubuntu经常断网.掉线.上不去网的原因,很可能是因为IPv6的关系,Ubuntu默认开启IPv6,在“设置--wifi--齿轮图标”中关掉就可以了. 经我环境测试,此方法无效 方案二: ...
- Django:视图views(一)
1.环境搭建 在django中,视图负责与web请求进行交互 视图本质上是一个Python函数,定义在booktest/views.py.通过django1/urls.py路由到该视图中. 首先经过创 ...
- zabbix设置中文并解决乱码问题
1.登录页面,设置中文 如下 2.解决乱码 进入本地PC的C:\Windows\Fonts,找到微软雅黑字体,复制粘贴,粘贴默认会生成两个文件 将msyh.ttf文件上传至zabbix服务器/usr/ ...
- 可执行代码(Executable Code)目标代码(object code)
小结: 1.可执行代码(Executable Code)是指将目标代码(object code)连接后形成的代码,简单来说是机器能够直接执行的代码. https://baike.baidu.com/i ...