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. C#里面的枚举与位或运算符 一个枚举变量存入多个值

    以前我们如果定义一个枚举类型 ,然后定义一个枚举变量,那么这个枚举变量只能为类型中的一个值,现在我们想要一个变量能够代表多个值: 今天看<Pro Net 2.0 Windows Forms An ...

  2. 浅谈weblogic与tomcat的区别

    weblogic是用于开发.集成.部署和管理大型分布式web应用.网络应用和数据库应用的java应用服务器,将java的动态功能和java enterprise标准的安全性引入大型网络应用的开发集成部 ...

  3. PS中进程状态

    PROCESS STATE CODES       Here are the different values that the s, stat and state output       spec ...

  4. 修复安卓的bug

    一直不明白listview中的复用,为什么会出现,项目多了.点击同一行的按钮,操作的不是指定行的对象. 仔细研读了代码,突然明白了.因为复用了,导致了position改变了. 伪码 if(conver ...

  5. eclipse 灵活使用makefile来编译C/C++

    需求: 近期在看<C++ Primer Plus>, 作者在不断优化自己的类.有很多不同的版本号,有非常多的測试函数(main函数),我使用的是eclipse+CDT来编写C++,不可能为 ...

  6. C#连接手机安装软件和发送信息

    今天突然想到怎么用winform程序连接自己的安卓手机,然后做发送短信的操作,查了很多资料 发现用ADB.exe 这个安卓自带的调试库 可以实现挺多功能的 前提是你自己手机已经和你电脑连接成功过,就是 ...

  7. div允许用户输入

    主要是用到contenteditable属性,就可以用div让用户输入了 <div id="guo" style="width:500px; height:200p ...

  8. 9款很酷炫jQuery/HTML5特效应用 有源码哦~

            目前最流行的网页特效应用当属jQuery和HTML5的特效应用了,它们可以帮你快速实现网页中的各种特效设计.本文就为了收集了9款非常酷炫的jQuery/HTML5特效应用,可以很方便的 ...

  9. CSS3 实现的一个简单的"动态主菜单" 示例

    其实这个示例蛮无聊的 很简单 也没什么实际的用处. 主要是展示了 CSS3 如何实现动画效果. 写这个主要是想看一看 完成这样的效果 我到底要写多少代码. 同时和我熟悉的java做个比较. 比较结果不 ...

  10. MFC显示bmp图像

    有了bmp文件读写的基础,我们就能够開始用MFC显示BMP图片了. 在这里,事实上微软为我们提供了一个实现bmp文件显示的框架,名叫diblook,我们能够先下载下来看看. 以下上链接:DIBLOOK ...