当前位置:网站首页>Guess the size of the number
Guess the size of the number
2022-06-25 06:13:00 【々 orange if ℃ №】
Learning goals :
A daily topic - Guess the size of the numbers
Learning content :
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 .
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 # The answer is in the interval [left, mid] in
else:
left = mid + 1 # The answer is in the interval [mid+1, right] in
# At this time there is left == right, The interval is reduced to a point , Is the answer
return left
边栏推荐
- Go uses channel to control concurrency
- Kyma application connectivity feature introduction
- Highway
- Research Report on global and Chinese vaccine market profit forecast and the 14th five year plan development strategy 2022-2028
- Jz-066- motion range of robot
- D compile time reflection
- Ethernet
- Huawei machine test question: splicing URL
- Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones
- MV command – move or rename files
猜你喜欢
Location object
[kicad image] download and installation
3-7sql injection website instance step 3: attack type and attack strategy
The simplest way to tell you is to hash and not hash
RT thread i/o device model and layering
ctfshow-misc
[Suanli network] technological innovation of Suanli Network -- Key Technologies of green and security
Introduction to sap ui5 tools
Tutorial 35 of SAP ui5 application development - how to deploy locally developed SAP ui5 applications to ABAP server for trial reading
Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones
随机推荐
SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control
Day21 performance test process
Processes and threads - concepts and process scheduling
Click to send text messages without response is a common problem for many users in building the elegant grass Dragonfly Q system - solve the problem of clicking to send text messages without response
Socket, network model notes
How SAP ui5 device type detection device API works
PAT (Advanced Level) Practice 1025
Ifconfig command – displays or sets network devices
Use generator-easy-ui5 to quickly create the engineering structure of SAP ui5 applications
What changes have taken place in the project file after SAP ui5 tools ran the Fiori add deploy config command
Digitalization, transformation?
JS implementation mouse can achieve the effect of left and right scrolling
Laravel8 fill data
[untitled]
Hands on deep learning (III)
ctfshow-misc
[Suanli network] technological innovation of Suanli Network -- Key Technologies of green and security
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
CSDN cerebral palsy bug has wasted nearly two hours of hard work
How often should you refactor- How often should you refactor?