当前位置:网站首页>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)
边栏推荐
- Tensorflow - tensorflow Foundation
- 一步教你溯源【钓鱼邮件】的IP地址
- 实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
- 20220610 other: Task Scheduler
- Leetcode - 5 longest palindrome substring
- Advantageous distinctive domain adaptation reading notes (detailed)
- 八、MySQL之事务控制语言
- Weight decay (pytorch)
- Raspberry pie 4B installs yolov5 to achieve real-time target detection
- Data classification: support vector machine
猜你喜欢

Ut2016 learning notes

Matrix calculation of Neural Network Introduction (pytoch)

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

Data preprocessing - Data Mining 1

Neural Network Fundamentals (1)

Ind kwf first week

Out of the box high color background system

Model selection for neural network introduction (pytorch)

多层感知机(PyTorch)
![[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
随机推荐
Leetcode刷题---1
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
神经网络入门之矩阵计算(Pytorch)
Leetcode - the k-th element in 703 data flow (design priority queue)
What can I do to exit the current operation and confirm it twice?
Ind kwf first week
Install yolov3 (Anaconda)
Preliminary knowledge of Neural Network Introduction (pytorch)
Julia1.0
Ut2016 learning notes
I really want to be a girl. The first step of programming is to wear women's clothes
ECMAScript--》 ES6语法规范 ## Day1
Leetcode刷题---367
[C question set] of Ⅵ
Boston house price forecast (tensorflow2.9 practice)
二分查找法
conda9.0+py2.7+tensorflow1.8.0
六、MySQL之数据定义语言(一)
Leetcode刷题---374
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning