当前位置:网站首页>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 - 11 <= 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;
}
边栏推荐
- arrayList && linkedList
- Misunderstanding of tongweb due to ease of use
- Power simple of leetcode-3
- Set the correct width and height of the custom dialog
- A fast week
- Tongweb adapts to openrasp
- Etcd fast cluster building
- Sentinel series introduction to service flow restriction
- Mongodb Multi - field Aggregation group by
- Working principle of sentinel series (concept)
猜你喜欢

Difference between deviation and variance in deep learning

Database design

Three paradigms of MySQL
![[China & some provinces and cities] JSON file for offline map visualization](/img/ea/0c552e1e133f693b9902c54c2e09c8.jpg)
[China & some provinces and cities] JSON file for offline map visualization

Explanation of sentinel series' features, composition and deployment

10 signalstartevent and signalcatchingevent of flowable signal events

Django uses redis to store sessions starting from 0

Byte buddy print execution time and method link tracking

Mongodb multi field aggregation group by

AUTOSAR实战教程pdf版
随机推荐
Summary of the 11th week of sophomore year
Feel the power of shardingsphere JDBC through the demo
若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
Pychart encountered time zone problem when connecting to MySQL timezone
Sentinel series integrates Nacos and realizes dynamic flow control
Solutions to conflicts between xampp and VMware port 443
Windbos common CMD (DOS) command set
2021.9.29 learning log MIME type
Let's talk about how ArrayList is dynamically resized and what kind of mechanism is it?
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
为什么那么多人讨厌A-Spice
11 signalthrowingevent and signalboundaryevent of flowable signal event
2021.9.29 learning log restful architecture
About Evaluation Metrics
Explanation of sentinel series' features, composition and deployment
2021.9.30 learning log -postman
No assessment summary
Problems encountered in the use of PgSQL
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
Top slide immersive dialog