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 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.
解题思路:
二分查找法的变体
代码如下:
# 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):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while(left <= right):
mid = (left + right) / 2
res = guess(mid)
if res == 0:
return mid
elif res == -1:
right = mid -1
elif res == 1:
left = mid + 1 return -1
Python [Leetcode 374]Guess Number Higher or Lower的更多相关文章
- 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 ...
- 【leetcode❤python】 374. Guess Number Higher or Lower
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...
- [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 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- [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 ...
- 【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 记录一次代码错误,elastic search的INDEX需要使用小写字母
Caused by: org.elasticsearch.hadoop.EsHadoopIllegalArgumentException: Cannot determine write shards ...
- [转]Linux下RPM软件包的安装及卸载 yum操作
在 Linux 操作系统下,几乎所有的软件均通过RPM 进行安装.卸载及管理等操作.RPM 的全称为Redhat Package Manager ,是由Redhat 公司提出的,用于管理Linux 下 ...
- 掌握这个Python小技巧,轻松构建cytoscape导入文件
今天小编和大家分享如何借助Python脚本轻松构建cytoscape导入文件.Cytoscape是一个非常适合展示各种相互作用关系的可视化软件. 具体来说就是可以用于蛋白互作网络的展示,miRNA与蛋 ...
- Spring AOP(3)
- Window修改git-bash默认路径
每次打开git-bash都默认到c盘,解决办法:修改git-bash的快捷方式 1. 删除目录后面的 --cd-to-home 2. 修改起始位置路径为你的项目路径 3. 还可以设置一个快捷键,在任何 ...
- 【bzoj3298】[USACO 2011Open]cow checkers(博弈论)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3298 博弈论经典结论题,我也没什么好说的.matrix67大佬比我想得深入的多:捡石子 ...
- 关于eclipse中看不到源码的问题
这几步之后会生成一个src压缩包,再用att打开即可.记住 要按ctrl再点击
- 记一次Configured Capacity: 0 (0 B)的解决
场景 最近hadoop集群新加了一个节点N,通过Ambari管理 一切正常. 过了两天发现,虽然集群每天要进几个G的数据(共8个节点),但节点N占用空间丝毫没有变化,显然没有进数据啊 日志 查看该节点 ...
- cn_03_r2_enterprise_sp2_x86_vl_X13_46432
1. 使用的 ISO为:cn_win_srv_2003_r2_enterprise_with_sp2_vl_cd1_X13-46432.iso 2.序列号 用的序列号是“DF74D-TWR86-D3F ...
- 数据库连接池 c3p0 druid
druid 数据库连接池 c3p0 使用C3P0数据源时需要依赖 mchange-commons-java-0.2.3.4.jar包.缺少该jar包则会报错!