当前位置:网站首页>LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
2022-07-04 14:00:00 【Tianchao龙虾】
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法—二分查找法)。
题目地址: https://leetcode.cn/problems/search-insert-position/
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
示例 1:
输入: nums = [1,3,5,6], target = 5
输出: 2
题目解法:
O(n)的写法:
一开始我的写法如下: 考虑整体遍历这个vector,然后看目标值是否小于等于这个当前值,是的话直接返回索引。
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;
}
};
但是看了题解之后,有如下写法:
O(logn)的写法:
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;
}
};
题目笔记
最上面的当然是可以得到结果,但是下面的优势在于,使用了二分查找的思想。但前提是这个数组是有序的。 前面当然是比较直观的,但是复杂度在O(n),二分查找只需要O(logn)。
边栏推荐
- Redis哨兵模式实现一主二从三哨兵
- 03-存储系统
- Redis 發布和訂閱
- Five minutes of machine learning every day: why do we need to normalize the characteristics of numerical types?
- Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
- Introduction to modern control theory + understanding
- They are all talking about Devops. Do you really understand it?
- Redis 发布和订阅
- UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
- flutter 报错 No MediaQuery widget ancestor found.
猜你喜欢

金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~

The performance of major mainstream programming languages is PK, and the results are unexpected

They are all talking about Devops. Do you really understand it?

Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?

How to match chords

Deep learning network regularization
![LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路](/img/4a/6763e3fbdeaf9de673fbe8eaf96858.png)
LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路

Guitar Pro 8win10 latest guitar learning / score / creation

numpy笔记

When synchronized encounters this thing, there is a big hole, pay attention!
随机推荐
左右对齐!
毕业季-个人总结
Is BigDecimal safe to calculate the amount? Look at these five pits~~
文本挖掘工具的介绍[通俗易懂]
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
Expose Ali's salary and position level
C1 certification learning notes 3 -- Web Foundation
TechSmith Camtasia studio 2022.0.2 screen recording software
LVLG 8.2 circular scrolling animation of a label
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
MySQL组合索引(多列索引)使用与优化案例详解
宽度精度
数据库函数的用法「建议收藏」
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Is it safe to open an account online for stock speculation? Will you be cheated.
2022 financial products that can be invested
【学习笔记】拟阵
Luo Gu - some interesting questions 2