[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 在给定的有序数组中插入一个目标数字,求出插入 ...
随机推荐
- Brunch:快捷的HTML5构建工具
Brunch,一个超快的HTML5构建工具.它可以(官方介绍): 编译你的脚本,模板,样式,链接它们, 将脚本和模板封装进common.js/AMD模块里,链接脚本和样式, 为链接文件生成源地图,复制 ...
- Java 编程的动态性,第 7 部分: 用 BCEL 设计字节码--转载
在本系列的最后三篇文章中,我展示了如何用 Javassist 框架操作类.这次我将用一种很不同的方法操纵字节码——使用 Apache Byte Code Engineering Library (BC ...
- 为什么要配置path环境变量?
一:关于path环境变量--为了在任意目录下,使用javac/java命令 第一种配置方法: 通过配置path环境变量,我们可以使某个程序,比如javac.exe,在任意目录下都可以运行,而不用跑到j ...
- linux定时任务2-at命令
定时执行命令: [root@rheltest1 ~]# at now + 1 minute //一分钟后执行下面的任务 at> date >> /tmp/test_date.log ...
- Vim的多窗口模式管理
Vim中的多窗口打开 vim中,默认的多窗口打开,是横向分割窗口. 进入vim编辑器以后,可以通过new命令,新建一个子窗口 :new “新建一个未命名窗口 :new name "新建一个 ...
- 让linux(centos)支持中文文件和文件夹
一.让linux支持中文 1.将Linux的env设置了LANG=en_US.UTF-8: 2.本地的Shell客户端编码也设置成UTF-8,这样让在windows上传到linux的文件或者目录不会出 ...
- 程序员必备英语.net版(.net菜鸟的成长之路-零基础到精通)
通过一段时间的.NET学习,我发现英文不好是我的软肋~我觉得好好补习一下英文单词水平.可是要背哪些单词呢? 经过一段时间的整理,终于整理出来了一套比较完整的.NET程序员必备单词文档.单词加详细说明. ...
- access 2007 vba (亖)
OpenReport方法执行 OpenReport 操作在 Visual Basic 中. 语法 表达式 .OpenReport(ReportName, View, FilterName, Whe ...
- jquery获取浏览器高度、宽度和滚动条高度(来自网络)
Jquery代码: alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(document).height()); //浏览器时下窗口文档的高度 ale ...
- [转载] 关于“淘宝应对"双11"的技术架构分析”
微博上一篇最新的关于“淘宝应对"双11"的技术架构分析”.数据产品的一个最大特点是数据的非实时写入.