当前位置:网站首页>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)。
边栏推荐
猜你喜欢

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

How to match chords

UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks

go-zero微服务实战系列(九、极致优化秒杀性能)

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?

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

Halcon knowledge: NCC_ Model template matching

flutter 报错 No MediaQuery widget ancestor found.

每周招聘|高级DBA年薪49+,机会越多,成功越近!

Helix swarm Chinese package is released, and perforce further improves the user experience in China
随机推荐
Ffmpeg Visual Studio development (IV): audio decoding
Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
Deep learning 7 transformer series instance segmentation mask2former
go-zero微服务实战系列(九、极致优化秒杀性能)
How to handle exceptions in multithreading?
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Memory management summary
局部修改-渐进型开发
5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
Quick introduction to automatic control principle + understanding
es6模块化
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
Helix swarm Chinese package is released, and perforce further improves the user experience in China
[learning notes] matroid
一篇文章搞懂Go语言中的Context
flutter 报错 No MediaQuery widget ancestor found.
Numpy notes
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~