当前位置:网站首页>LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
2022-07-04 15:13:00 【TianChao lobster】
LeetCode 35. Search insert location —vector Traverse (O(logn) and O(n) Writing — Dichotomy search ).
Title address : https://leetcode.cn/problems/search-insert-position/
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 .
Example 1:
Input : nums = [1,3,5,6], target = 5
Output : 2
Problem solving :
O(n) Writing :
At first, I wrote as follows : Consider traversing this as a whole vector, Then check whether the target value is less than or equal to the current value , If yes, return the index directly .
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.empty()) return 1;
const int size = nums.size();
for (size_t i=0; i<size; ++i){
if (target <= nums[i]) return i;
}
return size;
}
};
But after reading the solution , It's written as follows :
O(logn) Writing :
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.empty()) return 1;
const int size = nums.size();
int left=0, right=size-1;
while(left<=right){
auto middle = left+(right-left)/2;
if(nums[middle] <target)
left = middle+1;
else
right = middle -1;
}
return left;
}
};
Topic notes
Of course, the top one can get results , But the following advantages are , Using the idea of binary search . But the premise is that the array is ordered . Of course, the front is more intuitive , But the complexity is O(n), Binary search only needs O(logn).
边栏推荐
- The performance of major mainstream programming languages is PK, and the results are unexpected
- Halcon knowledge: NCC_ Model template matching
- Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
- PLC模拟量输入 模拟量转换FC S_ITR (CODESYS平台)
- 重排数组
- Enter the width!
- 进制乱炖
- 直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
- Ffprobe common commands
- Preliminary exploration of flask: WSGI
猜你喜欢
【大连理工大学】考研初试复试资料分享
Luo Gu - some interesting questions 2
C1 certification learning notes 3 -- Web Foundation
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
这几年爆火的智能物联网(AIoT),到底前景如何?
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
Deep learning network regularization
函数计算异步任务能力介绍 - 任务触发去重
Introduction to modern control theory + understanding
Flutter reports an error no mediaquery widget ancestor found
随机推荐
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
CentOS 6.3 下 PHP编译安装JSON模块报错解决
Numpy notes
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
华为云数据库DDS产品深度赋能
Halo effect - who says that those with light on their heads are heroes
Quick introduction to automatic control principle + understanding
这几年爆火的智能物联网(AIoT),到底前景如何?
压力、焦虑还是抑郁? 正确诊断再治疗
Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?
EventBridge 在 SaaS 企业集成领域的探索与实践
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Details of FPGA underlying resources
Unity脚本API—Transform 变换
Preliminary exploration of flask: WSGI
Weibo and Huya advance into interest communities: different paths for peers
信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
Unity脚本API—GameObject游戏对象、Object 对象
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧
数据库函数的用法「建议收藏」