当前位置:网站首页>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).
边栏推荐
- Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- Techsmith Camtasia Studio 2022.0.2屏幕录制软件
- Memory management summary
- 基于MAX31865的温度控制系统
- 函数计算异步任务能力介绍 - 任务触发去重
- Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
- Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
- Unity脚本API—Time类
- MySQL learning notes - data type (numeric type)
猜你喜欢
Quick introduction to automatic control principle + understanding
Details of FPGA underlying resources
音视频技术开发周刊 | 252
Optimization method of deep learning neural network
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
How did the beyond concert 31 years ago get super clean and repaired?
MySQL learning notes - data type (numeric type)
关于FPGA底层资源的细节问题
Deep learning network regularization
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
随机推荐
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
When synchronized encounters this thing, there is a big hole, pay attention!
力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
Redis哨兵模式实现一主二从三哨兵
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
Deep learning neural network case (handwritten digit recognition)
LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路
Redis sentinel mode realizes one master, two slave and three Sentinels
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
Weibo and Huya advance into interest communities: different paths for peers
Width and alignment
Unity脚本API—Time类
Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
一篇文章搞懂Go语言中的Context
近一亿美元失窃,Horizon跨链桥被攻击事件分析
.Net之延迟队列
31年前的Beyond演唱会,是如何超清修复的?
基于MAX31865的温度控制系统
怎么判断外盘期货平台正规,资金安全?