当前位置:网站首页>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

边栏推荐
- Data7202 statistical analysis
- How SAP ui5 device type detection device API works
- CSDN cerebral palsy bug has wasted nearly two hours of hard work
- Introduction to sap ui5 tools
- Digitalization, transformation?
- Wind farm visualization: wind farm data
- Introduction to the main features of kyma when the cloud native application runs
- Wireless industrial Internet of things data monitoring terminal
- Do you know what a three-tier architecture is?
- A + B Again
猜你喜欢
Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
SAP ui5 date type sap ui. model. type. Analysis of date parsing format

50 days countdown! Are you ready for the Landbridge cup provincial tournament?
MySQL transaction learning notes (I) first encounter

Es11 new methods: dynamic import(), bigint, globalthis, optional chain, and null value merging operator
SAP ui5 application development tutorial XXIX - Introduction to routing and navigation functions of SAP ui5 trial version

Processes and threads - concepts and process scheduling
![[Suanli network] problems and challenges faced by the development of Suanli network](/img/90/1d537de057113e2b4754e76746f256.jpg)
[Suanli network] problems and challenges faced by the development of Suanli network
Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
[kicad image] download and installation
随机推荐
TFTP command – uploading and downloading files
What is hybrid web containers for SAP ui5
How the sap ui5 framework performs single step debugging of batch requests
Rational investment and internationalism
Lesson 8: FTP server setup and loading
Laravel8 fill data
Socket, network model notes
Grep command – powerful text search tool
Understanding the dynamic mode of mongodb document
Data7202 statistical analysis
What happens when redis runs out of memory
Day22(File,DiGui,FileOutputStream)
Solve some prompt codes that pychar cannot recognize selenium
Leetcode topic [array] -36- effective Sudoku
Getting started with Silverlight development 1
The sum problem
Notes on dashboard & kuboard installation in kubernetes cluster
You can't specify target table for update in from clause error in MySQL
Digitalization, transformation?
What changes have taken place in the project file after SAP ui5 tools ran the Fiori add deploy config command