【题解】【数组】【查找】【Leetcode】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
思路:
其实二分查找是最基本的,本来没什么可说的,要命的是谈虎色变,条件稍微一变,一紧张边界情况就想不清楚大脑内存耗尽,写Search a 2D Matrix的时候,调了半天才发现程序是在二分处理只有两个元素的时挂了,写Median of Two Sorted Arrays的时候,又发现如果只有两个元素可能会造成死循环,所以担惊受怕的某程序媛决定采用这样的保守写法,小心地呵护trivial case,慎而试run之,大惊Accept
int searchInsert(int A[], int n, int target) {
int s = ;
int e = n-;
while(s < e- ){
int mid = (s+e)/;
if(target == A[mid]){
return mid;
}else if(target > A[mid]){
s = mid + ;
}else if(target < A[mid]){
e = mid - ;
}
}
if(s == e-){
if(target <= A[s]) return s;
else if(target > A[s+]) return s+;
else return s+;
}else if(s == e){
if(target <= A[s]) return s;
else return s+;
}
}
为了不愧对大脑内存正常的同学,奉上干货Matrix67的两篇文章《漫话二分》,并贴出一种正常的写法。。。跟经典的二分查找相比,只是多了一个条件:
int searchInsert(int A[], int n, int target) {
int l = , r = n-;
while(l <= r){
int mid = (l+r)/;
if(target == A[mid])
return mid;
if(mid > l && target < A[mid] && target > A[mid-])//比二分查找仅仅多了这句
return mid;
if(target < A[mid]){
r = mid-;
}else{
l = mid+;
}
}
return l;
}
【题解】【数组】【查找】【Leetcode】Search Insert Position的更多相关文章
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode Search Insert Position (二分查找)
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode Search Insert Position (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
- [LeetCode] Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode——Search Insert Position
Description: Given a sorted array and a target value, return the index if the target is found. If no ...
- [Leetcode] search insert position 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] Search Insert Position 二分搜索
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- leetcode Search Insert Position Python
#Given a sorted array and a target value, return the index if the target is found. If #not, return t ...
随机推荐
- Solr 4.3.0 配置Data import handler时出错
启动solr的时候,居然出现了如下的错误: org.apache.solr.common.SolrException: RequestHandler init failure at or ...
- Activity界面切换动画特效。
效果图: 结构图: 测试代码: 布局: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearL ...
- HTML5表单新增属性
1.form 原来html里面,表单里的元素应该包裹在表单里,如 <form action="login.php" method="get"> &l ...
- 知名杀毒软件Mcafee(麦咖啡)个人版 资源汇总兼科普(来自卡饭)
虽然早已不是用咖啡了,但我也实时关注的咖啡的一举一动,潜水看帖日久,发现小白众多,好多有价值的帖子淹没于帖海当中,甚是惋惜. 我有如下建议 1.咖啡区管理层,能否吧一些优秀的资源教程 ...
- 获取css style值
var obj=document.getElementById("btn");var currentStyle=null;if(obj.currentStyle){ current ...
- C语言中输入输出函数
1.1.1 格式化输入输出函数Turbo C2.0 标准库提供了两个控制台格式化输入. 输出函数printf() 和scanf(), 这两个函数可以在标准输入输出设备上以各种不同的格式读写数据.pri ...
- vSphere Client上传镜像
1. 使用vSphere Client连接到VMware ESXI 2. 打开右侧 [摘要] 选项卡 3. 在 [资源] 中选择存储器中的存储,右键 [浏览数据库存储] 4. 选择工具栏 [创建新文件 ...
- Integer 和int
获取Integer对象有两种方式:Integer x = 100:或者Integer x = new Integer(100): Integer x = 100:等价于Integer x = Inte ...
- poj2891 拓展欧几里得
//Accepted 164 KB 16 ms //拓展欧几里得 //m=a1*x+b1 --(1) //m=a2*(-y)+b2 --(2) //->a1*x+a2*y=b2-b1 //由欧几 ...
- JS 基于面向对象的 轮播图2
<script> // 函数不能重名, --> 子函数 // is defined function --> 函数名是否写错了 function AutoTab(id) { T ...