当前位置:网站首页>Leetcode guessing numbers game - simple
Leetcode guessing numbers game - simple
2022-06-13 05:48:00 【AnWenRen】
title :374 Guess the number game - Simple
subject
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
Code Java
public int guessNumber(int n) {
int start = 1;
int end = n;
while(start < end) {
// prevent int overflow
int mid = start + (end - start) / 2;
int x = guess(mid);
if (x == 0)
return mid;
else if (x < 0)
end = mid - 1;
else
start = mid + 1;
}
return start;
}
边栏推荐
- What happens when the MySQL union index ABC encounters a "comparison operator"?
- 16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
- 零拷贝技术
- 若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
- Class conflicts caused by tongweb Enterprise Edition and embedded Edition
- Application virtual directory static resource configuration on tongweb
- C calls the API and parses the returned JSON string
- 1 Introduction to drools rule engine (usage scenarios and advantages)
- OpenGL Mosaic (8)
- Use the browser to cut the entire page (take chrome as an example)
猜你喜欢
Config server configuration center of Nacos series
890. Find and Replace Pattern
NVIDIA Jetson nano/xavier NX capacity expansion tutorial
Building a stand-alone version of Nacos series
Nacos series registry principle and source code analysis
Problems encountered in the use of PgSQL
Service architecture diagram of Nacos series
3. Postman easy to use
OpenGL mosaic (VIII)
10 signalstartevent and signalcatchingevent of flowable signal events
随机推荐
OpenGL Mosaic (8)
MySQL main query and sub query
About Evaluation Metrics
Current limiting and fusing of gateway gateway in Spirng cloud
Explanation of sentinel series' features, composition and deployment
20 flowable container (event sub process, things, sub process, pool and pool)
JS output uincode code
Working principle of sentinel series (source code analysis)
Pyqt5 controls qpixmap, qlineedit qsplitter, qcombobox
设置自定义dialog的正确宽高
Why do so many people hate a-spice
Use the browser to cut the entire page (take chrome as an example)
Randomly fetch data from the list
Calculate the number of days between two times (supports cross month and cross year)
Getclassloader() returns null, getclassloader() gets null
How to view tongweb logs correctly?
The reason why the process cannot be shut down after a spark job is executed and the solution
3. Postman easy to use
Service fusing and degradation of Note Series
Three paradigms of MySQL