当前位置:网站首页>Leetcode skimming ---278
Leetcode skimming ---278
2022-07-03 10:35:00 【Long time no see 0327】
subject : You are the product manager , Currently leading a team to develop new products . Unfortunately , The latest version of your product doesn't pass the quality test . Because each version is based on the previous version , So all versions after the wrong version are wrong .
Suppose you have n A version [1, 2, ..., n], You want to find out the first wrong version that caused all subsequent versions to go wrong .
You can call bool isBadVersion(version) Interface to determine version number version If there is an error in unit test . Implement a function to find the first wrong version . You should try to minimize calls to API The number of times .
Input :n = 5, bad = 4
Output :4
Method 1 : Two points search
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int left = 1, right = n;
while (left < right) { // Cycle until the left and right end points of the interval are the same
int mid = left + (right - left) / 2; // Prevent calculation overflow
if (isBadVersion(mid)) {
right = mid; // The answer in [left, mid] in
} else {
left = mid + 1; // The answer in [mid, 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)
边栏推荐
猜你喜欢

8、 Transaction control language of MySQL

Ut2014 supplementary learning notes

Model selection for neural network introduction (pytorch)

Simple real-time gesture recognition based on OpenCV (including code)

LeetCode - 900. RLE iterator

Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation

Jetson TX2 brush machine

Training effects of different data sets (yolov5)

深度学习入门之线性代数(PyTorch)

What did I read in order to understand the to do list
随机推荐
CSDN, I'm coming!
Ut2014 supplementary learning notes
What can I do to exit the current operation and confirm it twice?
Secure in mysql8.0 under Windows_ file_ Priv is null solution
Leetcode刷题---374
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
SQL Server Management Studio cannot be opened
Hands on deep learning pytorch version exercise solution - 3.1 linear regression
Raspberry pie 4B installs yolov5 to achieve real-time target detection
Tensorflow—Neural Style Transfer
Model selection for neural network introduction (pytorch)
【毕业季】图匮于丰,防俭于逸;治不忘乱,安不忘危。
Judging the connectivity of undirected graphs by the method of similar Union and set search
High imitation Netease cloud music
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
一步教你溯源【钓鱼邮件】的IP地址
8、 Transaction control language of MySQL
An open source OA office automation system
Training effects of different data sets (yolov5)
Out of the box high color background system