当前位置:网站首页>【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;}
}
}
}
边栏推荐
- How will the complete NFT platform work in 2022? How about its core functions and online time?
- Gossip about redis source code 76
- Interesting 10 CMD commands
- Gossip about redis source code 80
- Selenium library 4.5.0 keyword explanation (4)
- [source code] VB6 chat robot
- 股票開戶傭金最低的券商有哪些大家推薦一下,手機上開戶安全嗎
- [MySQL] classification of multi table queries
- P1656 bombing Railway
- Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
猜你喜欢
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
[Mongodb] 2. Use mongodb --------- use compass
Tencent interview: can you find the number of 1 in binary?
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
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
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
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?
Common mode interference of EMC
Fluent learning (4) listview
随机推荐
2020.2.14
Tencent interview: can you find the number of 1 in binary?
Is user authentication really simple
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.
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
Social network analysis -social network analysis
Distributed transaction -- middleware of TCC -- selection / comparison
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
Pandaoxi's video
SQL data update
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
leetcode-43. String multiplication
Pytorch learning notes 5: model creation
2022.02.14
Gossip about redis source code 82
C summary of knowledge point definitions, summary notes
I wrote a chat software with timeout connect function
2022 system integration project management engineer examination knowledge points: software development model
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?