当前位置:网站首页>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)
边栏推荐
- 20220606 Mathematics: fraction to decimal
- Weight decay (pytorch)
- Yolov5 creates and trains its own data set to realize mask wearing detection
- Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
- Matrix calculation of Neural Network Introduction (pytoch)
- Ut2011 learning notes
- Inverse code of string (Jilin University postgraduate entrance examination question)
- 20220604 Mathematics: square root of X
- Rewrite Boston house price forecast task (using paddlepaddlepaddle)
- High imitation wechat
猜你喜欢

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

Ut2014 learning notes

Leetcode-513: find the lower left corner value of the tree

Softmax regression (pytorch)

Realize an online examination system from zero

LeetCode - 900. RLE iterator

Ut2014 supplementary learning notes

Leetcode - 5 longest palindrome substring

Handwritten digit recognition: CNN alexnet
![[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation](/img/02/35e73a43c4ad26fee9ecf180593464.jpg)
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
随机推荐
Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation
丢弃法Dropout(Pytorch)
Leetcode-513: find the lower left corner value of the tree
Jetson TX2 刷机
Are there any other high imitation projects
Leetcode刷题---10
安装yolov3(Anaconda)
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7
Ut2014 supplementary learning notes
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
一个30岁的测试员无比挣扎的故事,连躺平都是奢望
Multilayer perceptron (pytorch)
Data classification: support vector machine
Ut2015 learning notes
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
Synchronous vs asynchronous
Model selection for neural network introduction (pytorch)
Ind kwf first week
Knowledge map enhancement recommendation based on joint non sampling learning
QT creator uses OpenCV Pro add