当前位置:网站首页>【leetcode】374. Guess the size of the number
【leetcode】374. Guess the size of the number
2022-07-03 23:50:00 【Chinese fir sauce_】
subject :
374. Guess the size of the numbers
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
Dichotomy :
/** * Forward declaration of guess API. * @param num your guess * @return -1 if num is lower than the guess number * 1 if num is higher than the guess number * otherwise return 0 * int guess(int num); */
public class Solution extends GuessGame {
public int guessNumber(int n) {
int left = 1;
int right = n;
while(true){
int mid = (right - left) / 2 + left;
int ans = guess(mid);
if(ans == 1) left = mid + 1;
else if(ans == -1) right = mid;
else {
return mid;}
}
}
}
边栏推荐
- Fashion cloud interview questions series - JS high-frequency handwritten code questions
- Iclr2022: how does AI recognize "things I haven't seen"?
- Docking Alipay process [pay in person, QR code Payment]
- Idea set class header comments
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- How to quickly build high availability of service discovery
- 炒股开户佣金优惠怎么才能获得,网上开户安全吗
- I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
- Gossip about redis source code 73
- Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
猜你喜欢
![[note] IPC traditional interprocess communication and binder interprocess communication principle](/img/f6/36c28df02198539e27352e3cdf4ba6.jpg)
[note] IPC traditional interprocess communication and binder interprocess communication principle

Correlation analysis summary

2022 a special equipment related management (elevator) examination questions and a special equipment related management (elevator) examination contents

Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028

Similarities and differences of text similarity between Jaccard and cosine

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

How to quickly build high availability of service discovery

Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place

It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something

Idea a method for starting multiple instances of a service
随机推荐
NLP Chinese corpus project: large scale Chinese natural language processing corpus
Tencent interview: can you find the number of 1 in binary?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
[note] IPC traditional interprocess communication and binder interprocess communication principle
Gossip about redis source code 75
2022.02.13
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
How to make icons easily
Similarities and differences of text similarity between Jaccard and cosine
D24:divisor and multiple (divisor and multiple, translation + solution)
Maxwell equation and Euler formula - link
想请教一下,十大劵商如何开户?在线开户是安全么?
JarPath
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
[note] glide process and source code analysis
How about opening an account at Hengtai securities? Is it safe?
Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
Amway by head has this project management tool to improve productivity in a straight line
Investment demand and income forecast report of China's building ceramics industry, 2022-2028