本文为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. GDB调试工具总结

    程序调试的基本思想是“分析现象->假设错误原因->产生新的现象去验证假设”这样一个循环过程,根据现象如何假设错误原因,以及如何设计新的现象去验证假设,需要非常严密的分析和思考.程序中除了一 ...

  2. xUtils如何通过注解对FindViewById进行封装

    之前讲到了介绍了一下xUtils的基本使用方法,今天我们就来详细介绍一下关于xUtils中的ViewUtils模块. 在ViewUtils模块中我们首先看到的是它采用了一种注解的方式进行声明,那么我们 ...

  3. SQL Server 视图索引

    在视图上创建索引的另一个好处是:查询优化器开始在查询中使用视图索引,而不是直接在 FROM 子句中命令视图.这样一来,可从索引视图检索数据而无需重新编码,由此带来的高效率也使现有查询获益.在视图上创建 ...

  4. IDA 远程调试 Android so

      1.把ida 目录下android_server 传到android 目录中如:adb push  android_server /data/local/tmp/adb shell 进入模拟器cd ...

  5. Android直播实现 Android端推流、播放

    最近想实现一个Android直播,但是对于这方面的资料都比较零碎,一开始是打算用ffmpeg来实现编码推流,在搜集资料期间,找到了几个强大的开源库,直接避免了jni的代码,集成后只用少量的java代码 ...

  6. 剑指offer面试题12-打印1到最大的n位数

    题目: 输入一个数字n,按顺序打印出从1最大的n位十进制数.比方输入3,则打印出1.2.3最大的三位数即999 这道题的主要陷阱就在大数的处理,仅仅要将这个考虑进去,用字符串来表示.就好说了. 那差点 ...

  7. iOS单元測试:Specta + Expecta + OCMock + OHHTTPStubs + KIF

    框架选择 參考这篇选型文章,http://zixun.github.io/blog/2015/04/11/iosdan-yuan-ce-shi-xi-lie-dan-yuan-ce-shi-kuang ...

  8. Office安装错误1402的解决

    Office软件是我们工作是必备的,为了统一公司的办工软件,要把所有的WPS和Office2003版本全部换顾Office2010.在Win7下安装Office2010一般都不会存在什么大的问题,但遇 ...

  9. iOS中ActionSheet和Alert的区别

    首先,样子长得就不一样 看下图:

  10. PyQt5教程——布局管理(4)

    PyQt5中的布局管理 布局管理是GUI编程中的一个重要方面.布局管理是一种如何在应用窗口上防止组件的一种方法.我们可以通过两种基础方式来管理布局.我们可以使用绝对定位和布局类. 绝对定位 程序指定了 ...