当前位置:网站首页>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)
边栏推荐
- Leetcode刷题---75
- 多层感知机(PyTorch)
- 20220601 Mathematics: zero after factorial
- 七、MySQL之数据定义语言(二)
- Leetcode-112: path sum
- Realize an online examination system from zero
- 【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
- Rewrite Boston house price forecast task (using paddlepaddlepaddle)
- Leetcode刷题---374
- I really want to be a girl. The first step of programming is to wear women's clothes
猜你喜欢

Multilayer perceptron (pytorch)

MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

What can I do to exit the current operation and confirm it twice?

权重衰退(PyTorch)

侯捷——STL源码剖析 笔记

Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation

Ut2017 learning notes
![[LZY learning notes -dive into deep learning] math preparation 2.1-2.4](/img/92/955df4a810adff69a1c07208cb624e.jpg)
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4

Configure opencv in QT Creator

Neural Network Fundamentals (1)
随机推荐
Ind FXL first week
A super cool background permission management system
Yolov5 creates and trains its own data set to realize mask wearing detection
Leetcode刷题---367
High imitation wechat
What useful materials have I learned from when installing QT
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
[LZY learning notes dive into deep learning] 3.5 image classification dataset fashion MNIST
Free online markdown to write a good resume
2-program logic
Are there any other high imitation projects
Synchronous vs asynchronous
Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
Ut2012 learning notes
Drop out (pytoch)
20220608 other: evaluation of inverse Polish expression
Leetcode刷题---704
[C question set] of Ⅵ
Tensorflow—Image segmentation