当前位置:网站首页>【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;}
}
}
}
边栏推荐
- P1339 [USACO09OCT]Heat Wave G
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Gossip about redis source code 78
- Distributed transaction -- middleware of TCC -- selection / comparison
- Briefly understand the operation mode of developing NFT platform
- Idea set class header comments
- Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
- D24:divisor and multiple (divisor and multiple, translation + solution)
- "Learning notes" recursive & recursive
- How to write a good title of 10w+?
猜你喜欢
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
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
The difference between single power amplifier and dual power amplifier
Gorilla/mux framework (RK boot): add tracing Middleware
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Interesting 10 CMD commands
2022 a special equipment related management (elevator) examination questions and a special equipment related management (elevator) examination contents
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Selenium check box
Pyqt5 sensitive word detection tool production, operator's Gospel
随机推荐
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
JDBC Technology
[MySQL] sql99 syntax to realize multi table query
Pyqt5 sensitive word detection tool production, operator's Gospel
Gossip about redis source code 76
Gossip about redis source code 79
C # basic knowledge (3)
Gossip about redis source code 82
A treasure open source software, cross platform terminal artifact tabby
Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
D26: the nearest number (translation + solution)
D27:mode of sequence (maximum, translation)
Gossip about redis source code 83
After the Lunar New Year and a half
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Ramble 72 of redis source code
[Mongodb] 2. Use mongodb --------- use compass
How to prevent malicious crawling of information by one-to-one live broadcast source server
Actual combat | use composite material 3 in application
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place