374. Guess Number Higher or Lower [easy] (Python)

本文介绍了LeetCode上的猜数字游戏问题,玩家需要在1到n的范围内猜测一个选定的数字。每次猜测错误,会得到反馈是数字更高或更低。文章提供两种解题思路:一是使用二分搜索法,二是通过递归算法。并给出了相应的Python代码实现。
摘要由CSDN通过智能技术生成

题目链接

https://leetcode.com/problems/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.

题目翻译

猜数字游戏。游戏规则是这样的:我从1到n中挑选一个数,你来猜我挑的数是多少。每次你猜错了我都会告诉你正确数字是更大还是更小。
你写的函数可以调用一个预先实现的guess(int num)的接口,它能返回-1,0,1三个可能值:-1表示正确数字更小;1表示正确数字更大;0表示猜对了。
比如:当n=10,我选的数是6时&#x

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值