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的更多相关文章

  1. 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 ...

  2. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  4. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

  5. [LeetCode] Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. LeetCode——Search Insert Position

    Description: Given a sorted array and a target value, return the index if the target is found. If no ...

  7. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. [LeetCode] Search Insert Position 二分搜索

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. [LeetCode] Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  10. 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 ...

随机推荐

  1. http://ecgui.com/?hg=0&nr=0

    eCGUI- 微型嵌入式GUI体积小巧 大小在 100KB~180KB! 特性: 多窗口支持| 完全中文输入/显示| 多数常用GUI控件| 99.9% ANSI C 编写| 已成功移植 DOS,Lin ...

  2. PDF 补丁丁 0.4.1 版:新增嵌入中文字库、替换文档字库的功能

    PDF 补丁丁 0.4.1 版新增了嵌入中文字库.替换文档字库的功能. 嵌入汉字字库 历史上有一批黄底黑字的 PDF 文档.这批文档都具有相同的问题:没有嵌入字库.在一些设备上阅读时显示乱码.复制文本 ...

  3. 如何实现301的跳转?当输入域名http://xxx.com的时候自动重定向到www上去

    答案:在服务器上操作,注意勾选和不勾选的区别,使用Fiddle进行观察,301和302之间的区别

  4. Split的应用

    public string qu(string ss) { string s1 = "" ; string[] s = ss.Split(); for (int i = 0; i ...

  5. 【bzoj3160】万径人踪灭

    题意:给一个只含a.b的字符串,求所有的回文不连续子序列. manacher+FFT. 先求出所有回文序列,再减去连续子序列(即回文串). 将a.b分开考虑,对于一个对称轴,以其为回文中心的回文序列的 ...

  6. lazyload 分页加载

    http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/ echo $d['pic']; ?>" src=&qu ...

  7. PHP后台

    一.ajax提交表单 先引入ajax.js function ajax(url, fnSucc, fnFaild) { //1.创建Ajax对象 var oAjax=null; if(window.X ...

  8. NDK JNI 的关键点

    1.System.loadLibrary 的名字是在Android.mk里面设定的   LOCAL_MODULE    := httpdown,MODULE   后面跟的就是了 2.如何正确调用到关键 ...

  9. [开发笔记]-js判断用户的浏览设备是移动设备还是PC

    最近做的一个网站页面中需要根据用户的访问设备的不同来显示不同的页面样式,主要是判断移动设备还是电脑浏览器访问的. 下面给出js判断处理代码,以作参考. <script type="te ...

  10. getBoundingClientRect() 来获取页面元素的位置

    getBoundingClientRect() 来获取页面元素的位置   document.documentElement.getBoundingClientRect 下面这是MSDN的解释: Syn ...