当前位置:网站首页>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
边栏推荐
- 编译,连接 -- 笔记
- One brush 313 sword finger offer 06 Print linked list from end to end (E)
- Recommended search common evaluation indicators
- 基础贪心总结
- Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
- Config组件学习笔记
- 多线程原理和常用方法以及Thread和Runnable的区别
- MySQL集群模式與應用場景
- 小程序知识点积累
- 负载均衡器
猜你喜欢
Independent operation smart farm Innovation Forum
C language --- string + memory function
Pgpool II 4.3 Chinese Manual - introductory tutorial
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
Spark 算子之coalesce与repartition
Why is IP direct connection prohibited in large-scale Internet
R语言中实现作图对象排列的函数总结
Single architecture system re architecture
Why disable foreign key constraints
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
随机推荐
MySQL集群模式与应用场景
leetcode-396 旋转函数
What if the server is poisoned? How does the server prevent virus intrusion?
C, calculation method and source program of bell number
Spark 算子之partitionBy
utils.DeprecatedIn35 因升级可能取消,该如何办
fatal error: torch/extension. h: No such file or directory
字符串排序
Accumulation of applet knowledge points
计算某字符出现次数
Pgpool II 4.3 Chinese Manual - introductory tutorial
leetcode-374 猜数字大小
What if the package cannot be found
JVM - Chapter 2 - class loader subsystem
vim指定行注释和解注释
C#,贝尔数(Bell Number)的计算方法与源程序
Calculate the number of occurrences of a character
字符串最后一个单词的长度
js正则判断域名或者IP的端口路径是否正确
Treatment of idempotency