当前位置:网站首页>Leetcode-374 guess the size of the number
Leetcode-374 guess the size of the number
2022-04-23 15:48:00 【Mid year and early year boundary】
The rules of the number guessing game are as follows :
Every round of the game , I'll start from 1 To n Randomly choose a number . Please guess which number is chosen .
If you guessed wrong , I'll tell you , Is your guess larger or smaller than the number I selected .
You can call a predefined interface int guess(int num) To get a guess , The total number of return values is 3 A possible situation (-1,1 or 0):
-1: The number I picked is smaller than your guess pick < num
1: The number I picked was bigger than you guessed pick > num
0: I picked the same number as you guessed . Congratulations ! You guessed it !pick == num
Back to the number I picked .
Example 1:
Input :n = 10, pick = 6
Output :6
Example 2:
Input :n = 1, pick = 1
Output :1
Example 3:
Input :n = 2, pick = 1
Output :1
Example 4:
Input :n = 2, pick = 2
Output :2
Tips :
1 <= n <= 231 - 1
1 <= pick <= n
solution :
# 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: int) -> int:
class Solution:
def guessNumber(self, n: int) -> int:
left, right = 1, n
while left < right:
mid = (left + right) // 2
if guess(mid) <= 0:
right = mid
else:
left = mid + 1
return left
版权声明
本文为[Mid year and early year boundary]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231545223056.html
边栏推荐
- Configuration of multi spanning tree MSTP
- 怎么看基金是不是reits,通过银行购买基金安全吗
- 多生成树MSTP的配置
- 提取不重复的整数
- 【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
- 【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
- Interview questions of a blue team of Beijing Information Protection Network
- CAP定理
- js正則判斷域名或者IP的端口路徑是否正確
- Temporal model: long-term and short-term memory network (LSTM)
猜你喜欢
随机推荐
vim指定行注释和解注释
Upgrade MySQL 5.1 to 5.611
Basic greedy summary
Date date calculation in shell script
Neodynamic Barcode Professional for WPF V11. 0
Why disable foreign key constraints
IronPDF for .NET 2022.4.5455
One brush 313 sword finger offer 06 Print linked list from end to end (E)
New developments: new trends in cooperation between smartmesh and meshbox
Go language, condition, loop, function
Merging of Shanzhai version [i]
The principle and common methods of multithreading and the difference between thread and runnable
Redis主从复制过程
多级缓存使用
utils.DeprecatedIn35 因升级可能取消,该如何办
时序模型:门控循环单元网络(GRU)
单体架构系统重新架构
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
Go并发和通道