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. NetworkComms网络通信框架V3结构图

    NetworkComms网络通信框架序言 来自英国的c#网络通信框架,历时五年打造,由英国剑桥的2位工程师倾情开发,最新版本V3.x版本.

  2. spring mvc读取url变量

    @RequestMapping(value="/{id}/{name}", method=RequestMethod.GET) public ModelAndView getUrl ...

  3. HTML元素,属性,基础标签

    元素,属性 元素 html有父元素和子元素,被包含的叫子元素,如html是head的父元素,他们是父子关系,head和body是兄弟关系 <html> <head></h ...

  4. 使用HTTP访问网络------使用HTTPURLConnection

    HTTPURLConnection继承了URLConnection,因此也可用于向指定网站发送GET请求.POST请求.它在URLConnection的基础上提供了如下便捷的方法: 1.int  ge ...

  5. java之代理模式

    静态代理: java代理是一种模式--代理模式.采用代理模式,可以在不改变目标类代码的基础上,通过代理对象,来增加额外的功能(比如增加日志检测等)或者只需要目标对象的部分行为. java中,代理分为静 ...

  6. CF 103E Buying Sets 最大权闭合子图,匹配 难度:4

    http://codeforces.com/problemset/problem/103/E 这道题首先一看就很像是最大权闭合子图,但是我们可以认为现在有两种点,数字和集合点,我们需要消除数字点的影响 ...

  7. JSON.NET 教程(一)

    下载地址:http://www.newtonsoft.com/json 参考官网文档:http://www.newtonsoft.com/json/help/html/SerializingJSON. ...

  8. wp8.1 Study13:在WP8.1中分享文件和数据

    绪论:不同于windows, 在wp8.1中,如果不止一个程序可以接受其Uri或者文件,shell会提供一个界面让用户选择用哪个程序.而在windows中,用户可以在设置那里设置各种文件和Uri的默认 ...

  9. CCNA 6.5

    no sh (no shutdown : start the interface)   router rspf 1 network x.x.x.x x.x.x.x area 0 int (interf ...

  10. android Xutils dbutils 注解

    xUtils DbUtils 关于实体类注解 汇总 RockyZhang 发布于 1年前,共有 0 条评论 先来官方demo DbUtils db = DbUtils.create(this);    ...