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

思路:

与普通二分搜索的唯一区别在于 可能插入的位置是没找到时 l 的值

int searchInsert(int A[], int n, int target) {
int l = , r = n - ;
while(l <= r)
{
int m = (l + r) / ;
if(A[m] == target)
return m;
else if(A[m] > target)
r = m - ;
else
l = m + ;
}
return l; //注意 就这里不一样 最后r = l - 1 返回l就可以了 就是应该插入的位置
}

【leetcode】 search Insert Position(middle)的更多相关文章

  1. 【LeetCode】- Search Insert Position(查找插入的位置)

    [ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...

  2. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. 【题解】【数组】【查找】【Leetcode】Search Insert Position

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

  4. 【LeetCode】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  5. 【leetcode】Search Insert Position

    题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  6. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  7. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  9. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

随机推荐

  1. 【MongoDB】MongoDb的“not master and slaveok=false”错误及解决方法

    链接mongodb报错如下 2016-03-14T16:26:00.912+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok& ...

  2. HTML Table导出为Excel的方法

    HTML Table导出为Excel的方法: 直接上源码 <html> <head> <meta http-equiv="Content-Type" ...

  3. Javascript软键盘设计

    国内大多数网站的密码在网络传输过程中都是明文的,我们目前正在做的产品也是这样的情形,这正常吗? 大家都偷懒?不重视安全?各人持有观点,有人认为明文传输并不是想象中的那么可怕,事实上正常情况下这些报文你 ...

  4. 文字的多列布局--column

  5. springmvc之图片上传

    1.接收到的是图片的流时 //上传头像 @RequestMapping(value = "/uploadHeadSculpture", method = RequestMethod ...

  6. Js控制iFrame切换加载网址

    <html> <head> <title>Js控制 iFrame 切换加载网址</title> </head> <body> & ...

  7. C#画表格

    下面给一个简单的例子,至于多个单元格合并,请自己去实现,也就是坐标计算的事情. 至于画图,用GDI,还是DirectX画,自己选择,不过这里主要讲的是算法:坐标计算以及画的过程. 注意不要每个列都画一 ...

  8. Pythhon 字典 key in dict 比 dict.has_key (key)效率高 为什么?

    has_key是去取key对应的值,时间复杂度在最优情况下为O(1); in 是直接去dict.__contains__这个保存这key的list中去获取,相当与是去数组中获取. 所以in 比has_ ...

  9. 2016 网易校招内推C/C++第二场8.6

    选择题20个,每个1.5,编程题3个,每个20,简答题1个10分. 解: 第二题,一开始喵了一眼,好开心,这不是水题么,第一反应想到的是递归,然后马上就写了,结果case10%,一脸蒙蔽,数据值很大, ...

  10. PHPExcel类的使用讲解

    下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...