当前位置:网站首页>【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;}
}
}
}
边栏推荐
- Fudan 961 review
- Common mode interference of EMC
- 2022 a special equipment related management (elevator) examination questions and a special equipment related management (elevator) examination contents
- Ramble 72 of redis source code
- Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
- Selenium library 4.5.0 keyword explanation (4)
- leetcode-43. String multiplication
- 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
- No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
- How can I get the Commission discount of stock trading account opening? Is it safe to open an account online
猜你喜欢

Actual combat | use composite material 3 in application

I wrote a chat software with timeout connect function

Kubedl hostnetwork: accelerating the efficiency of distributed training communication

Enter MySQL in docker container by command under Linux

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

Similarities and differences of text similarity between Jaccard and cosine

Iclr2022: how does AI recognize "things I haven't seen"?
![Docking Alipay process [pay in person, QR code Payment]](/img/30/665580241020ee3adb872e725f0624.jpg)
Docking Alipay process [pay in person, QR code Payment]

It is forbidden to splice SQL in code

A method to solve Bert long text matching
随机推荐
Make small tip
Vscode regular match replace console log(.*)
JarPath
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
C # basic knowledge (2)
Zipper table in data warehouse (compressed storage)
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Interesting 10 CMD commands
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?
P1656 bombing Railway
2020.2.14
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.
Common mode interference of EMC
NLP Chinese corpus project: large scale Chinese natural language processing corpus
P3371 [template] single source shortest path (weakened version)
Design of logic level conversion in high speed circuit
Gossip about redis source code 74
[note] glide process and source code analysis
Idea set class header comments
P1339 [USACO09OCT]Heat Wave G