本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Search Insert Position

Total Accepted: 14279 Total
Submissions: 41575

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(log n),空间O(1)

相关题目:

Search a 2D Matrix

class Solution {
public:
    int searchInsert(int A[], int n, int target) {
    int s = 0, e = n , m ; //中间数,偶数个的时候取前一个
    while(s < e){
    m = s + (e - s) / 2;
    if(A[m] == target) return m;
    if(A[m] < target) s = m + 1;
    if(A[m] > target) e = m;
    }
    if(A[m] > target) return m;
    else return m + 1;
    }
};

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

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

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

  2. 【LeetCode】35. Search Insert Position (2 solutions)

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

  3. leetcode 二分查找 Search in Rotated Sorted ArrayII

    Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...

  4. leetcode 二分查找 Search in Rotated Sorted Array

    Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...

  5. Leetcode No.35 Search Insert Position(c++实现)

    1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...

  6. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  7. LeetCode OJ 35. 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 Problem 35:Search Insert Position

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

  9. 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 ...

随机推荐

  1. Java中浮点类型的精度问题 double float

    要说清楚Java浮点数的取值范围与其精度,必须先了解浮点数的表示方法与浮点数的结构组成.因为机器只认识01,你想表示小数,你要机器认识小数点这个东西,必须采用某种方法.比如,简单点的,float四个字 ...

  2. Debug时检测到Loaderlock的解决办法

    昨天遇到了Loaderlock的问题. 出错信息为:检测到LoaderLock,正试图在OS加载程序锁内执行托管代码,不要尝试在DllMain或映像初始化函数内运行托管代码,这样会导致应用程序挂起. ...

  3. Java Object Clone

    Java Object Clone User user = new User(); user.setName("tom"); User user1 = new User(); us ...

  4. 机器学习的开源平台 TensorFlow

    一. google第二代人工智能机器学习开源工具. http://www.tensorfly.cn/ 二. 知乎上关于机器学习的资料问答 https://www.zhihu.com/question/ ...

  5. Ubuntu 或 UbuntuKyLin14.04 Unity桌面側边栏和顶层菜单条显示异常解决方法

    近期一直想要回到Linux以下去开发,正好Ubuntu14.04桌面版系统在近期公布,所以趁此机会下载了最新版的Ubuntu版本号.并在虚拟机VMware软件上面搭建一改Ubuntu系统,因为眼下有适 ...

  6. Mac下Sublime Text 总是以新窗口打开文件的解决办法

    Mac下的Sublime有个毛病,经常打开后,之前打开的窗口都没了,太难受了. Windows/Linux下的sublime总是默认的以标签页的形式打开关联的文件,但是在Mac下使用Sublime打开 ...

  7. iOS8开发~Swift(二)Playground

    一.Playground介绍 Playground是Xcode6中自带的Swift代码开发环境.俗话说"功欲善其事,必先利其器".曾经在Xcode5中编写脚本代码.比如编写JS.其 ...

  8. 如何使用Total Recorder录制软件发出的声音

    1 打开Total Recorder的选项,点击系统设置,在弹出的声音选项卡中把Total Recorder扬声器设为默认(选中该项再点击默认,如果第一个扬声器选项还保存着"默认通信设备&q ...

  9. SqlServer日常积累(二)

    1.Like运算符:将字符串表达式与 SQL表达式中的模式进行比较匹配. 语法 :expression Like 'pattern' ,expression为匹配字段,pattern为匹配字符串.可以 ...

  10. fiddler设置显示区域参数

    oSession["ui-color"] = "red"; 设置字体颜色,颜色名称oSession["ui-italic"] = " ...