当前位置:网站首页>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).
边栏推荐
- Decimal, exponential
- 暑期复习,一定要避免踩这些坑!
- Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
- selenium 浏览器(2)
- 对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
- Align left and right!
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- 局部修改-渐进型开发
- Techsmith Camtasia Studio 2022.0.2屏幕录制软件
- Five minutes of machine learning every day: why do we need to normalize the characteristics of numerical types?
猜你喜欢

产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现

大神详解开源 BUFF 增益攻略丨直播

Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example

智能客服赛道:网易七鱼、微洱科技打法迥异

函数计算异步任务能力介绍 - 任务触发去重

Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked

Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
![LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路](/img/4a/6763e3fbdeaf9de673fbe8eaf96858.png)
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路

从0到1建设智能灰度数据体系:以vivo游戏中心为例
随机推荐
unity update 协程_Unity 协程的原理
Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
都在说DevOps,你真正了解它吗?
十六进制
js平铺数据查找叶子节点
内存管理总结
宽度与对齐
Preliminary exploration of flask: WSGI
Redis的4种缓存模式分享
局部修改-渐进型开发
文本挖掘工具的介绍[通俗易懂]
openresty 限流
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
Partial modification - progressive development
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
They are all talking about Devops. Do you really understand it?
找数字
Width accuracy
Temperature control system based on max31865
selenium 元素交互