当前位置:网站首页>Leetcode skimming ---35
Leetcode skimming ---35
2022-07-03 10:35:00 【Long time no see 0327】
subject : Given a sort array and a target value , Find the target value in the array , And return its index . If the target value does not exist in the array , Return to where it will be inserted in sequence . Must use a time complexity of O(log n) The algorithm of .
Input :nums = [1,3,5,6], target = 5
Output :2
Method 1 : Two points search
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
int left = 0, right = n - 1, ans = n;
while (left <= right) {
int mid = ((right - left) >> 1) + left;
if (target <= nums[mid]) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
};
Complexity analysis
Time complexity :O(log n)
Spatial complexity :O(1)
边栏推荐
- Leetcode - 1172 plate stack (Design - list + small top pile + stack))
- Numpy Foundation
- 20220607 others: sum of two integers
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
- Leetcode-404: sum of left leaves
- Ind FHL first week
- Ind kwf first week
- What can I do to exit the current operation and confirm it twice?
- Ind wks first week
猜你喜欢
Leetcode-112: path sum
LeetCode - 900. RLE iterator
Hands on deep learning pytorch version exercise solution - 2.4 calculus
Realize an online examination system from zero
Introduction to deep learning linear algebra (pytorch)
High imitation Netease cloud music
Standard library header file
7、 Data definition language of MySQL (2)
Multilayer perceptron (pytorch)
Boston house price forecast (tensorflow2.9 practice)
随机推荐
Leetcode刷题---278
Ut2017 learning notes
Simple real-time gesture recognition based on OpenCV (including code)
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
Notes - regular expressions
GAOFAN Weibo app
Boston house price forecast (tensorflow2.9 practice)
ThreadLocal原理及使用场景
20220604 Mathematics: square root of X
Leetcode刷题---75
Rewrite Boston house price forecast task (using paddlepaddlepaddle)
What useful materials have I learned from when installing QT
Leetcode刷题---10
Data preprocessing - Data Mining 1
Hands on deep learning pytorch version exercise solution -- implementation of 3-2 linear regression from scratch
丢弃法Dropout(Pytorch)
Standard library header file
Secure in mysql8.0 under Windows_ file_ Priv is null solution
Hands on deep learning pytorch version exercise solution - 2.4 calculus
深度学习入门之自动求导(Pytorch)