[LeetCode] 35. Search Insert Position 解决思路
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.[1,3,5,6]
, 5 → 2[1,3,5,6]
, 2 → 1[1,3,5,6]
, 7 → 4[1,3,5,6]
, 0 → 0
问题:给定一个已排序数组和一个整数,若整数已在数组中则返回在数组中的下标,否则返回应当插入的位置。
对一个已排序数组进行搜索,很自然地会想到二分搜索(Binary Search),毕竟是经典场景。这道题也确实是二分搜索的一个简单应用。
之所以记录这道题目,是感觉二分搜索和之前做的 双指针法 two pointers ,或者是滑动窗口算法(sliding window) 有些相似。
二分搜索,实际上,可以理解为每次减半的滑动窗口算法,来定位最终的目标位置。
而滑动窗口算法,另一个典型场景就是求解最大/最小 的连续子数组,或连续子字符串。在另一篇博文有介绍:Minimum Size Subarray Sum 。
int searchInsert(vector<int>& nums, int target) { if (nums.size() == ) {
return ;
} if (nums.size() == ) {
return (nums[] < target) ? : ;
} int l = ;
int r = (int)nums.size() - ; while (l < r) { if (l + == r) {
if ( target <= nums[l]) {
return l;
}
else if (target <= nums[r]){
return r;
}
else{
return r+;
}
} int mid = (l + r) / ; if (nums[mid] == target) {
return mid;
} if (nums[mid] < target) {
l = mid;
}else{
r = mid;
}
} // 实际上无法执行到这里,在前面必然有 return.
return ;
}
[LeetCode] 35. Search Insert Position 解决思路的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- [LeetCode] 35. Search Insert Position ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路 Given a sorted array ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- Java [leetcode 35]Search Insert Position
题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- LeetCode 35. Search Insert Position (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [leetcode]35. Search Insert Position寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
随机推荐
- CentOS 6.3下rsync服务器的安装与配置[转]
CentOS 6.3下rsync服务器的安装与配置 一.rsync 简介 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也 ...
- seq2sparse(4)之PartialVectorMergeReducer源码分析
继前篇blogseq2sparse(3)之TFParitialVectorReducer源码分析 之后,继续分析下面的代码,本次分析的是PartialVectorMergeReducer的源码,这个r ...
- ubuntu 下操作文件夹,出现Permission denied的解决的方法
今天遇到个诡异问题,向一个文件夹(myResources)粘贴文件的时候,出现这样一个提示 Permission denied 是权限没设好,仅仅是拷贝粘贴一个文件,怎么会这样? 解决的办法: $ s ...
- iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
iis 7上发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 提示里面的解决方法是: 如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. 使用 II ...
- FFMPEG 视频旋转设置
fmpeg -i inputfile.mp4 -vf "transpose=1" outputfile.mp4 0=90CounterCLockwise and Vertical ...
- sql - 修改结构
1,修改表名 语法: sp_rename old_table_name, new_table_name 例如: sp_rename t_review, t_business 2,修改字段: MySQL ...
- Linux中 pid_t 类型的定义.
说明:涉及到的头文件(.h),目录默认都是基于 /usr/include/ 目录. 1.在 "/sys/types.h"中,有下列内容: #include <bits/typ ...
- SQL 关于有单引号数据更新的问题
要把sql语句中包含有单引号的符号加入到数据库中的做法 )),''','123.com') 很简单就是加入id=''123'' 0'0就可以写成'0''0'
- Java中的内部类、匿名类的使用
代码(test.java): interface ie{ public void print(); } class outer{} public class test{ public class in ...
- C++拾遗(八)类——概念、定义与实现
Class与Struct 区别在于class默认访问类型是private,struct默认访问类型是public. 另外在使用习惯上,struct只用来表示纯粹的数据对象或没有私有部分的类. 类中的内 ...