当前位置:网站首页>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
边栏推荐
- Application of Bloom filter in 100 million flow e-commerce system
- 【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
- Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
- 【第5节 if和for】
- 负载均衡器
- Spark 算子之partitionBy
- Upgrade MySQL 5.1 to 5.67
- Advantages, disadvantages and selection of activation function
- 字符串最后一个单词的长度
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
猜你喜欢

Neodynamic Barcode Professional for WPF V11. 0

网站压测工具Apache-ab,webbench,Apache-Jemeter

Spark 算子之sortBy使用

MySQL集群模式与应用场景

API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)

MySQL Cluster Mode and application scenario

现在做自媒体能赚钱吗?看完这篇文章你就明白了

WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令

时序模型:门控循环单元网络(GRU)

一刷314-剑指 Offer 09. 用两个栈实现队列(e)
随机推荐
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
utils.DeprecatedIn35 因升级可能取消,该如何办
Cookie&Session
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
CAP定理
字符串排序
Modèle de Cluster MySQL et scénario d'application
Mumu, go all the way
Neodynamic Barcode Professional for WPF V11. 0
北京某信护网蓝队面试题目
Extract non duplicate integers
What if the package cannot be found
Accumulation of applet knowledge points
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
The principle and common methods of multithreading and the difference between thread and runnable
c语言---指针进阶
Recommended search common evaluation indicators
Temporal model: long-term and short-term memory network (LSTM)
Cap theorem
What if the server is poisoned? How does the server prevent virus intrusion?