当前位置:网站首页>【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;}
}
}
}
边栏推荐
- D30:color tunnels (color tunnels, translation)
- D25:sequence search (sequence search, translation + problem solving)
- Double efficiency. Six easy-to-use pychar plug-ins are recommended
- How can I get the Commission discount of stock trading account opening? Is it safe to open an account online
- Fashion cloud interview questions series - JS high-frequency handwritten code questions
- How to make recv have a little temper?
- P1629 postman delivering letter
- 2022.02.14
- Gossip about redis source code 79
- Comment obtenir une commission préférentielle pour l'ouverture d'un compte en bourse? Est - ce que l'ouverture d'un compte en ligne est sécurisée?
猜你喜欢

Selenium check box

2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit

The interviewer's biggest lie to deceive you, bypassing three years of less struggle

"Learning notes" recursive & recursive

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

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

Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method

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

Enter MySQL in docker container by command under Linux

Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
随机推荐
Maxwell equation and Euler formula - link
Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
SPI based on firmware library
How to quickly build high availability of service discovery
Advanced C language - pointer 2 - knowledge points sorting
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
Pandaoxi's video
NPM script
C # basic knowledge (1)
P3371 [template] single source shortest path (weakened version)
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
URLEncoder. Encode and urldecoder Decode processing URL
2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
Op amp related - link
Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
2022.02.13
Gossip about redis source code 77
Les sociétés de valeurs mobilières dont la Commission d'ouverture d'un compte d'actions est la plus faible ont ce que tout le monde recommande.
What is the Valentine's Day gift given by the operator to the product?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?