[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 在给定的有序数组中插入一个目标数字,求出插入 ...
随机推荐
- [Angular 2] Using a Reducer to Change an Object's Property Inside an Array
Reducers are also often used for changing a single property inside of other reducers. This lesson sh ...
- java Serializable和Externalizable序列化反序列化详解--转
一.什么是序列化? “对象序列化”(Object Serialization)是 Java1.1就开始有的特性. 简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存 ...
- 最全的C#图片处理帮助类ImageHelper
最全的C#图片处理帮助类ImageHelper.cs 方法介绍: 生成缩略图 图片水印处理方法 图片水印位置处理方法 文字水印处理方法 文字水印位置的方法 调整光暗 反色处理 浮雕处理 拉伸图片 滤色 ...
- PC端QQ协议解析之0825
QQ协议0825代号解析,包括客户端发送包和服务器发送包. 主要借鉴的此篇文章,我自己也是重复造轮子. 基本信息 操作系统:windows7 QQ-Version:3643 客户端到服务器: 02:数 ...
- C#几种截取字符串的方法小结,需要的朋友可以参考一下
1.根据单个分隔字符用split截取 例如 复制代码 代码如下: string st="GT123_1"; string[] sArray=st.split("_&quo ...
- jQuery入门必须掌握的一些API
jQuery 中文版文档:http://www.css88.com/jqapi-1.9/category/ajax/ jQuery入门,必须掌握以下的API,平时工作中经常会用到.未列出的API,在掌 ...
- 检查DOM能力的函数
var a=document.implementation.hasFeature("Core","2.0"); var b=document.implement ...
- java学习笔记 (9) —— Struts2 国际化
1.Test.java package com.i18n; import java.util.Locale; public class Test1 { public static void main( ...
- 流Stream个人学习理解
1.Stream类 命名空间:System.IO 程序集:mscorlib 流是对字节序列的抽象,提供字节序列的一般视图. 流的操作包括三个方面: 1.读取(Read):将流数据传入到数据结构 2.写 ...
- winform中相对路径和绝对路径的获取
例如: Path.GetFullPath(Path.Combin(@"C:\a\b\c","..\b.text")); ..代表上级目录 .代表当前目录 结果: ...