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

最常见的思路:顺序查找,顺序找到第一个大于等于带插入数据的元素的位置,时间复杂度O(N),注意到题目中原数组是有序的,且是查找问题,自然想到二分查找。

二分查找的代码如下:

int BinaryResearch(int A[],int low,int high,int target)
{
int l = low;
int h = high;
while(l<=h)
{
int mid = (int)((h+l)/2);
if(A[mid]==target) return mid;
else if(A[mid]<target) l = mid+1;
else h = mid-1;
}
return -1;
}

二分查找运用到此题目中时,若待插入数据在原数组中可以查找到(原数组中有与待插入数据相同的数),则插入到这个位置即可,对应到上述代码中查找成功return mid;即可;

关键在于插入数据在不在原数组中的时候,怎么考虑呢?在二分查找中,如果查找失败,则一定是low>high的情况。例如在数组[1,2,3,4,5,6,8,9]中查找7,那么返回时,其low指针指向8的位置,而high指针指向6的位置,即low指向第一个大于查找目标的数,high指向比7小的最大的数,显然low的位置便是插入位置。

时间复杂度为O(logN)。

代码:

int searchInsert(int A[], int n, int target) {
int low = 0;
int hig = n-1;
while(low<=hig)
{
int mid = (int)((low+hig)/2);
if(A[mid]==target) return mid;
else if(A[mid]<target) low = mid+1;
else hig = mid-1;
}
return low;
}

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][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  3. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  4. Leetcode35 Search Insert Position 解题思路(python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...

  5. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  6. leetcode-algorithms-35 Search Insert Position

    leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...

  7. 【leetcode】35-Search Insert Position

    problem Search Insert Position 一种容易想到的是暴力破解法,一种是常用的二分法. 暴力破解法1(不推荐) class Solution { public: int sea ...

  8. 乘风破浪:LeetCode真题_035_Search Insert Position

    乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...

  9. LeetCode: Search Insert Position 解题报告

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

随机推荐

  1. HeadFisrt 设计模式03 装饰者

    类应该对扩展开放, 对修改关闭. 所谓装饰者模式, 是指用其他的类来装饰某个类, 装饰者说白了就是使用 has-a 来代替 is-a 隐喻 咖啡店, 有很多种咖啡, 咖啡里还要增加一些 milk, 面 ...

  2. jquery可拖动表格调整列格子的宽度大小(转)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. google cloud之停止任务

    1.点击侧边栏:ML Engine -> Jobs 2.选择对应的任务,点击stop

  4. 002Maven_第一个Maven演示

    第一步. 首先建立Hello项目同时建立Maven约定的目录结构 Hello     --src     -----main     ----------java     ----------reso ...

  5. MyBatis 使用简单的 XML或注解用于配置和原始映射

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .My ...

  6. 下列JSP代码:

    下列JSP代码: <html> <body> <% for(int i = 0; i < 10; i++) { //1 } %> </body> ...

  7. php -- 可变变量

    有时候使用可变变量名是很方便的.就是说,一个变量的变量名可以动态的设置和使用.一个普通的变量通过声明来设置,例如: <?php $a = 'hello'; ?> 一个可变变量获取了一个普通 ...

  8. WORD里怎样能做到局部“分栏”就是一页里有的分有的不分

    选中你要分的部分再分栏如果不想分的部分也被分了,那就可以选中不想分的那部分,选择“分栏”->“一栏” 转自:http://zhidao.baidu.com/question/9873268.ht ...

  9. 插件之下拉框Select2

    select2为代替常规的select而出现,可自定义select的样式,最明显的功能就是集合中可以搜索 关于浏览器要求,ie8+,Chrome 8+,Firefox 10+,Safari 3+,Op ...

  10. 如何用ChemDraw选择结构

    在使用ChemDraw软件过程中,我们往往会需要对结构进行翻滚.连结.旋转.分解.组合等操作.但是对于新手用户来说不是每种操作大家都会使用的. 针对这种情况我们会有一系列的教程来为大家讲解.下面我们就 ...