当前位置:网站首页>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
边栏推荐
猜你喜欢

Spark 算子之filter使用

WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile

山寨版归并【上】

Do we media make money now? After reading this article, you will understand

Modèle de Cluster MySQL et scénario d'application

Treatment of idempotency

Spark 算子之sortBy使用

CVPR 2022 quality paper sharing

Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant

实现缺省页面
随机推荐
Merging of Shanzhai version [i]
布隆过滤器在亿级流量电商系统的应用
What if the package cannot be found
Spark 算子之filter使用
Timing model: gated cyclic unit network (Gru)
Modèle de Cluster MySQL et scénario d'application
Interview questions of a blue team of Beijing Information Protection Network
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
Upgrade MySQL 5.1 to 5.611
多线程原理和常用方法以及Thread和Runnable的区别
Spark 算子之交集、并集、差集
字符串最后一个单词的长度
实现缺省页面
CAP定理
PHP operators
Upgrade MySQL 5.1 to 5.69
Application case of GPS Beidou high precision satellite time synchronization system
C language --- advanced pointer
VIM specifies the line comment and reconciliation comment
Mumu, go all the way