当前位置:网站首页>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).
边栏推荐
- Weibo and Huya advance into interest communities: different paths for peers
- EventBridge 在 SaaS 企业集成领域的探索与实践
- go-zero微服务实战系列(九、极致优化秒杀性能)
- 左右对齐!
- Redis 发布和订阅
- [differential privacy and data adaptability] differential privacy code implementation series (XIV)
- 深度学习 神经网络的优化方法
- Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
- flutter 报错 No MediaQuery widget ancestor found.
- TechSmith Camtasia studio 2022.0.2 screen recording software
猜你喜欢

Optimization method of deep learning neural network

MP3是如何诞生的?

Go zero micro service practical series (IX. ultimate optimization of seckill performance)

深度学习 神经网络案例(手写数字识别)
MySQL组合索引(多列索引)使用与优化案例详解

31年前的Beyond演唱会,是如何超清修复的?

Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!

Unity动画Animation Day05

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?

Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
随机推荐
web聊天室实现
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
They are all talking about Devops. Do you really understand it?
Preliminary exploration of flask: WSGI
Unity update process_ Principle of unity synergy
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!
Leecode learning notes - Joseph problem
近一亿美元失窃,Horizon跨链桥被攻击事件分析
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
Unity脚本API—Time类
都在说DevOps,你真正了解它吗?
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Deep learning network regularization
十六进制
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
EventBridge 在 SaaS 企业集成领域的探索与实践
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
Optimization method of deep learning neural network
AI做题水平已超过CS博士?