当前位置:网站首页>【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;}
}
}
}
边栏推荐
- Introduction to the gtid mode of MySQL master-slave replication
- Distributed transaction -- middleware of TCC -- selection / comparison
- Fudan 961 review
- P1339 [USACO09OCT]Heat Wave G
- Correlation analysis summary
- 2.14 summary
- D27:mode of sequence (maximum, translation)
- P1656 bombing Railway
- [note] IPC traditional interprocess communication and binder interprocess communication principle
- ADB related commands
猜你喜欢

China standard gas market prospect investment and development feasibility study report 2022-2028

Idea a method for starting multiple instances of a service

Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities

2022.02.14

2022.02.13

NLP Chinese corpus project: large scale Chinese natural language processing corpus

Tencent interview: can you find the number of 1 in binary?
![[Mongodb] 2. Use mongodb --------- use compass](/img/d5/0eb7dd4c407fbf2e9ba1b175f5424d.jpg)
[Mongodb] 2. Use mongodb --------- use compass

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

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
随机推荐
Idea integrates Microsoft TFs plug-in
[source code] VB6 chat robot
D24:divisor and multiple (divisor and multiple, translation + solution)
Pandaoxi's video
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
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
Briefly understand the operation mode of developing NFT platform
Learning methods of zynq
2/14 (regular expression, sed streaming editor)
Live app source code, jump to links outside the station or jump to pages inside the platform
Pytorch learning notes 5: model creation
What is the Valentine's Day gift given by the operator to the product?
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?
Maxwell equation and Euler formula - link
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
NPM script
P1339 [USACO09OCT]Heat Wave G
"Learning notes" recursive & recursive