【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/guess-number-higher-or-lower/#/description
题目描述
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.
题目大意
从1~n中取了一个数字,现在给出了guess()函数,要你猜这个数字是多少。
解题方法
做这个题的重点是明白guess()函数,题目说了是我取了一个数字,你去猜这个数字,guess()是我的数字大了还是小了。。明白这个意思了么。
所以这个题是标准的二分查找。
/* The guess API is defined in the parent class GuessGame.
@param num, your guess
@return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num); */
public class Solution extends GuessGame {
public int guessNumber(int n) {
int low = 1;
int high = n;
int mid = 0;
while(low <= high){
mid = low + (high - low) / 2;
if(guess(mid) == -1){
high = mid - 1;
}else if(guess(mid) == 1){
low = mid + 1;
}else{
return mid;
}
}
return mid;
}
}
python解法如下:
# 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 #[left, right]
mid = left
while left <= right:
mid = (right + left) / 2
res = guess(mid)
if res == 0:
return mid
elif res == 1:
left = mid + 1
else:
right = mid - 1
return mid
日期
2017 年 5 月 10 日
2018 年 11 月 23 日 —— 这就星期五了??
【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)的更多相关文章
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 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 ...
- 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 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】933. Number of Recent Calls 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- [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 选择一个数字. 你 ...
随机推荐
- 如何利用nrfjprog.exe读写nrf51的flash
版权声明:本文为博主原创文章,未经博主允许不得转载. 1.目的 为了方便平时在开发中的调试,验证一些想法是否正确. 2.平台: Jlink version:v5.02c nrf51822硬件板等. ...
- c#表中信息点击跳转
OnRowCommand="gridInfoData_RowCommand" <Columns> <asp:ButtonField HeaderText=&quo ...
- A Child's History of England.7
After the death of Ethelbert, Edwin, King of Northumbria [公元616年,隋朝末年], who was such a good king tha ...
- UBI 文件系统之分区挂载
Linux 系统中有关mtd和ubi的接口:(1) cat /proc/mtd:可以看到当前系统的各个mtd情况,(2) cat /proc/partitions: 分区信息,有上面的类似(3) ca ...
- 商业爬虫学习笔记day6
一. 正则解析数据 解析百度新闻中每个新闻的title,url,检查每个新闻的源码可知道,其title和url都位于<a></a>标签中,因为里面参数的具体形式不一样,同一个正 ...
- java 多线程的状态迁移 常用线程方法分析
一.线程的各个状态 图中的线程状态(Thread.Stat 中定义的Enum 名)NEW.RUNNABLE .TERMINATED.WAITING.TIMED_WAITING 和BLOCKED 都能够 ...
- 过滤敏感词工具类SensitiveFilter
网上过滤敏感词工具类有的存在挺多bug,这是我自己改用的过滤敏感词工具类,目前来说没啥bug,如果有bug欢迎在评论指出 使用前缀树 Trie 实现的过滤敏感词,树节点用静态内部类表示了,都写在一个 ...
- HDC2021技术分论坛:如何高效完成HarmonyOS分布式应用测试?
作者:liuxun,HarmonyOS测试架构师 HarmonyOS是新一代的智能终端操作系统,给开发者提供了设备发现.设备连接.跨设备调用等丰富的分布式API.随着越来越多的开发者投入到Harmon ...
- ciscn_2019_s_9
很简单的一道题 例行检查 没有开启nx保护,就想到了shellcode来做 很明显的栈溢出 唯一的要求就是shellcode长度不能超过0x24 通过jump跳转到shellcode的位置 完整exp ...
- 数据类型Table.TransformColumnTypes(Power Query 之 M 语言)
数据源: 任意数据源 目标: 设置适合的数据类型 操作过程: 选取指定列>[主页]>[数据类型]>选取 选取指定列>[转换]>[数据类型]>选取 选取指定列> ...