当前位置:网站首页>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)
边栏推荐
- Ut2014 supplementary learning notes
- Leetcode刷题---10
- Hands on deep learning pytorch version exercise solution - 2.6 probability
- Inverse code of string (Jilin University postgraduate entrance examination question)
- Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
- Standard library header file
- What can I do to exit the current operation and confirm it twice?
- Leetcode skimming ---217
- Synchronous vs asynchronous
- Data preprocessing - Data Mining 1
猜你喜欢

侯捷——STL源码剖析 笔记

Leetcode - 5 longest palindrome substring

6、 Data definition language of MySQL (1)

Secure in mysql8.0 under Windows_ file_ Priv is null solution

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

Free online markdown to write a good resume

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

An open source OA office automation system

丢弃法Dropout(Pytorch)

神经网络入门之矩阵计算(Pytorch)
随机推荐
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
Weight decay (pytorch)
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
Out of the box high color background system
20220601 Mathematics: zero after factorial
20220602 Mathematics: Excel table column serial number
Notes - regular expressions
Leetcode刷题---278
20220606 Mathematics: fraction to decimal
权重衰退(PyTorch)
Ind FXL first week
2-program logic
High imitation Netease cloud music
6、 Data definition language of MySQL (1)
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
[C question set] of Ⅵ
Neural Network Fundamentals (1)
Inverse code of string (Jilin University postgraduate entrance examination question)
R language classification