当前位置:网站首页>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)
边栏推荐
- 安装yolov3(Anaconda)
- Realize an online examination system from zero
- Pytorch ADDA code learning notes
- Codeup: word replacement
- 重写波士顿房价预测任务(使用飞桨paddlepaddle)
- Ut2011 learning notes
- Numpy Foundation
- 六、MySQL之数据定义语言(一)
- Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
- Weight decay (pytorch)
猜你喜欢

Judging the connectivity of undirected graphs by the method of similar Union and set search

Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation

ThreadLocal原理及使用场景

多层感知机(PyTorch)

Leetcode - 5 longest palindrome substring

Multilayer perceptron (pytorch)

七、MySQL之数据定义语言(二)

Matrix calculation of Neural Network Introduction (pytoch)

Ut2014 learning notes

2018 y7000 upgrade hard disk + migrate and upgrade black apple
随机推荐
Policy gradient Method of Deep Reinforcement learning (Part One)
一个30岁的测试员无比挣扎的故事,连躺平都是奢望
Seata分布式事务失效,不生效(事务不回滚)的常见场景
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
Leetcode刷题---374
Several problems encountered in installing MySQL under MAC system
I really want to be a girl. The first step of programming is to wear women's clothes
High imitation Netease cloud music
20220531 Mathematics: Happy numbers
Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
conda9.0+py2.7+tensorflow1.8.0
Ut2014 learning notes
Standard library header file
Advantageous distinctive domain adaptation reading notes (detailed)
Step 1: teach you to trace the IP address of [phishing email]
QT creator uses OpenCV Pro add
Weight decay (pytorch)
Automatic derivation of introduction to deep learning (pytoch)