当前位置:网站首页>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).
边栏推荐
- 2022 financial products that can be invested
- 重排数组
- numpy笔记
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
- LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
- 信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
- 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?
- LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
- web聊天室实现
- IO flow: node flow and processing flow are summarized in detail.
猜你喜欢
Introduction to asynchronous task capability of function calculation - task trigger de duplication
IO流:节点流和处理流详细归纳。
The performance of major mainstream programming languages is PK, and the results are unexpected
Flutter reports an error no mediaquery widget ancestor found
Huawei cloud database DDS products are deeply enabled
Redis 解决事务冲突之乐观锁和悲观锁
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
开源人张亮的 17 年成长路线,热爱才能坚持
关于FPGA底层资源的细节问题
科研漫画 | 联系到被试后还需要做什么?
随机推荐
How to handle exceptions in multithreading?
web聊天室实现
openresty 限流
hexadecimal
AI做题水平已超过CS博士?
numpy笔记
深度学习 神经网络的优化方法
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
Partial modification - progressive development
MySQL学习笔记——数据类型(2)
unity update 协程_Unity 协程的原理
重排数组
科研漫画 | 联系到被试后还需要做什么?
深度学习 网络正则化
函数计算异步任务能力介绍 - 任务触发去重
[local differential privacy and random response code implementation] differential privacy code implementation series (13)
IO flow: node flow and processing flow are summarized in detail.
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
微博、虎牙挺进兴趣社区:同行不同路
Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?