[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 在给定的有序数组中插入一个目标数字,求出插入 ...
随机推荐
- PC端QQ协议解析之0825
QQ协议0825代号解析,包括客户端发送包和服务器发送包. 主要借鉴的此篇文章,我自己也是重复造轮子. 基本信息 操作系统:windows7 QQ-Version:3643 客户端到服务器: 02:数 ...
- Shell基本的命令
ubuntu 中文乱码 如果使用的是 PuTTY,可以通过修改 font, character set 设置来解决. Window -> Appearance -> Font settin ...
- nginx 1.安装
nginx 1.安装 nginx的众多优点这里就不多说了,直接开始吧. 基本依赖 yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel ...
- Asp.net 回车默认按钮
<head> <script type="text/javascript"> function OnKey() { if (ev ...
- 【转】node-webkit:开发桌面+WEB混合型应用的神器
顾名思义,node-webkit就是nodejs+webkit. 这样做的好处显而易见,核心奥义在于,用nodejs来进行本地化调用,用webkit来解析和执行HTML+JS. 快速上手 下载node ...
- IKAnalyzer原理分析
IKAnalyzer原理分析 IKAnalyzer自带的 void org.wltea.analyzer.dic.Dictionary.disableWords(Collection<Strin ...
- mysql 存储引擎MYSIAM和INNODB特性比较
事物:MYISAM不支持事物,MyISAM类型的表强调的是性能,其执行数度比InnoDB 类型更快.如果不考虑事物,大量的select和insert适合MYISAM表 锁:MYISAM支持表锁 ...
- UIWebView禁止点击后跳转
#pragma mark 禁止webview中的链接点击 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRe ...
- php 通过ip获取地理位置
<?php header('Content-Type:text/html;Charset=utf-8'); function GetIp(){ $realip = ''; $unknown = ...
- phpcms v9 数据库分离部署
v9数据模型功能,允许用户把不同的数据表,分离到不同的数据库服务器上.以实现负载的分离,更加的符合大访问网站的需求. <ignore_js_op> 数据分离方法 1.数据库连接配置配置文件 ...