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


1.二分查找时间复杂度O(logn)。

二分查找的基本思想是将n个元素分成大致相等的两部分,去a[n/2]与x做比较,如果x=a[n/2],则找到x,算法中止;如果x<a[n/2],则只要在数组a的左半部分继续搜索x,如果x>a[n/2],则只要在数组a的右半部搜索x.

时间复杂度无非就是while循环的次数!

总共有n个元素,

渐渐跟下去就是n,n/2,n/4,....n/2^k,其中k就是循环的次数

由于你n/2^k取整后>=1

即令n/2^k=1

可得k=log2n,(是以2为底,n的对数)

所以时间复杂度为O(logn)

代码:

class Solution {
public:
int searchInsert(int A[], int n, int target) {
int l=;
int r=n-;
int mid;
while (l<=r)
{
mid=(l+r)/;
if(A[mid]>target) r=mid-;
else if(A[mid]<target) l=mid+;
else return mid;
}
if(l==r+)
return l;
else if(r=-)
return ;
else if(l=n)
return n;
}
};

2.一般方法,复杂度O(n),先找出边界,然后就是相等和两数之间的情况。

class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(target>A[n-]) return n;
if(target<A[]) return ;
for(int i=;i<n;++i)
{
if(A[i]==target) return i;
if(i<(n-)&&target>A[i]&&target<A[i+])
return i+;
}
}
};

Search Insert Position(二分查找)的更多相关文章

  1. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

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

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

  3. 35. 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(搜索查找位置)

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

  5. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

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

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

  7. LeetCode OJ: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,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 ...

  9. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

  10. [Leetcode][Python]35: Search Insert Position

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

随机推荐

  1. MySQL学习随笔--通过实例理解merge ,temptable算法的差异

    实例1 使用视图的两种算法merge和temptable分别统计 表tb_phone中market_price大于4000的手机,然后查询视图查找出小于6000的手机 简单总结最终获取的结果:查询出m ...

  2. 无聊的我写了一个代码 。。。P1605 迷宫

    搜索水题 哎 直接不行了 . #include <ctype.h> #include <cstdio> void read(int &x) { x=;char ch=g ...

  3. druid数据库连接池整合到SpringMvc

    1.maven项目加入相关的依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>dru ...

  4. mysql 查看存储过程 并导出

    查询数据库中的存储过程 select * from mysql.proc where db = dbName and `type` = 'PROCEDURE' show procedure statu ...

  5. C#委托与事件的关系(转载)

    1.C#中的事件和委托的作用?事件代表一个组件能够被关注的一种信号,比如你的大肠会向你发出想拉屎的信号,你就可以接收到上厕所.委托是可以把一个过程封装成变量进行传递并且执行的对象,比如你上蹲坑和上坐马 ...

  6. CAD使用SetxDataDouble写数据(com接口)

    主要用到函数说明: MxDrawEntity::SetxDataDouble 写一个Double扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据 ...

  7. 除了上万的月薪之外,还有什么理由让我们必须学Python?

    虽然目前的编程语言有很多,但是基础语法上的概念,本质上都是相通的.可以做到一通百通.所以没有必要为了学哪门语言纠结太多. python是目前市面上,我个人认为是最简洁&&最优雅& ...

  8. zeng studio的项目窗口PHP Explorer

    恢复zeng studio的项目窗口PHP Explorer方法: Windows>show view >PHP Explorer

  9. Libjingle 库

    Libjingle 是google talk voice(语音聊天) 和 p2p interoperability(点对点操作)库,是提供了google talk,p2p文件共享和语音呼叫能力的组件集 ...

  10. vue 封装自定义组件

    组件结构 sjld >index.js >sjid.vue 最好单独放一个文件夹,有依赖的话装依赖 Sjld.vue 内容 <template id="sjld" ...