当前位置:网站首页>【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;}
}
}
}
边栏推荐
- Gorilla/mux framework (RK boot): add tracing Middleware
- Open 2022 efficient office, starting from project management
- Kubedl hostnetwork: accelerating the efficiency of distributed training communication
- Selenium library 4.5.0 keyword explanation (III)
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- [network security] what is emergency response? What indicators should you pay attention to in emergency response?
- 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
- Iclr2022: how does AI recognize "things I haven't seen"?
- Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
- P1339 [USACO09OCT]Heat Wave G
猜你喜欢

Idea a method for starting multiple instances of a service

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

Current detection circuit - including op amp current scheme

Schematic diagram of crystal oscillator clock and PCB Design Guide
![[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections](/img/c6/3dc7d01600f6713afdbb4cf3df5238.jpg)
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections

SPI based on firmware library

Is user authentication really simple

2022.02.14

Alibaba cloud container service differentiation SLO hybrid technology practice

Idea set class header comments
随机推荐
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
Ramble 72 of redis source code
URLEncoder. Encode and urldecoder Decode processing URL
NLP Chinese corpus project: large scale Chinese natural language processing corpus
Introducing Software Testing
Introduction to the gtid mode of MySQL master-slave replication
Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
Fudan 961 review
JDBC Technology
Gossip about redis source code 82
Recursive least square adjustment
[MySQL] classification of multi table queries
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028
[source code] VB6 chat robot
A method to solve Bert long text matching
Schematic diagram of crystal oscillator clock and PCB Design Guide
D25:sequence search (sequence search, translation + problem solving)
Gossip about redis source code 79
Gorilla/mux framework (RK boot): add tracing Middleware