当前位置:网站首页>Leetcode skimming ---374
Leetcode skimming ---374
2022-07-03 10:35:00 【Long time no see 0327】
subject : 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 .
Input :n = 10, pick = 6
Output :6
Method 1 : Two points search
class Solution {
public:
int guessNumber(int n) {
int left = 1, right = n;
while (left < right) { // Cycle until the left and right breakpoints of the interval are the same
int mid = left + (right - left) / 2; // Prevent overflow during calculation
if (guess(mid) <= 0) {
right = mid; // The answer is in the interval [left, mid] in
} else {
left = mid + 1; // The answer is in the interval [mid+1, right] in
}
}
// At this time there is left == right, The interval is reduced to a point , Is the answer
return left;
}
};Complexity analysis
Time complexity :O(logn)
Spatial complexity :O(1)
边栏推荐
- R language classification
- Stroke prediction: Bayesian
- Preliminary knowledge of Neural Network Introduction (pytorch)
- Drop out (pytoch)
- 20220602 Mathematics: Excel table column serial number
- 20220531 Mathematics: Happy numbers
- Powshell's set location: unable to find a solution to the problem of accepting actual parameters
- 20220606 Mathematics: fraction to decimal
- MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
- Leetcode-100: same tree
猜你喜欢
![Step 1: teach you to trace the IP address of [phishing email]](/img/a5/c30bc51da560c4da7fc15f434dd384.png)
Step 1: teach you to trace the IP address of [phishing email]

Yolov5 creates and trains its own data set to realize mask wearing detection

ECMAScript--》 ES6语法规范 ## Day1

神经网络入门之预备知识(PyTorch)

Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition

一个30岁的测试员无比挣扎的故事,连躺平都是奢望

Realize an online examination system from zero

重写波士顿房价预测任务(使用飞桨paddlepaddle)

Policy gradient Method of Deep Reinforcement learning (Part One)

Neural Network Fundamentals (1)
随机推荐
Seata分布式事务失效,不生效(事务不回滚)的常见场景
Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
重写波士顿房价预测任务(使用飞桨paddlepaddle)
20220603 Mathematics: pow (x, n)
Ut2013 learning notes
一步教你溯源【钓鱼邮件】的IP地址
Leetcode刷题---35
Data preprocessing - Data Mining 1
神经网络入门之矩阵计算(Pytorch)
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
Training effects of different data sets (yolov5)
I really want to be a girl. The first step of programming is to wear women's clothes
Judging the connectivity of undirected graphs by the method of similar Union and set search
2018 y7000 upgrade hard disk + migrate and upgrade black apple
High imitation bosom friend manke comic app
Data classification: support vector machine
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
Timo background management system
深度学习入门之自动求导(Pytorch)
An open source OA office automation system