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

跟定一个有序数组和一个目标,如果目标在数组里找到目标的下标,如果不在,找到目标应该插入的位置。

二分查找,找到则返回mid,否则返回r+1或则l。

class Solution {
public:
int searchInsert(int A[], int n, int target)
{
if (n == 0)
return 0;
int l = 0, r = n - 1, mid = 0;
while(l <= r)
{
mid = (l + r) / 2;
if (A[mid] == target)
return mid;
if (A[mid] < target)
l = mid + 1;
else
r = mid - 1;
}
return l; // 返回的是l,或者是r+1
}
};

2015/03/31:

从左到右找到相等的或者找到第一次比target大的数的位置即为插入下标位置,但这个是On的,python如下:

class Solution:
# @param A, a list of integers
# @param target, an integer to be inserted
# @return integer
def searchInsert(self, A, target):
for i in range(len(A)):
if A[i] == target or A[i] > target:
return i
return len(A)

还是用第一种 二分法快吧

leetcode 34 Search Insert Position的更多相关文章

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

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

  2. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  3. LeetCode 035 Search Insert Position

    题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...

  4. [LeetCode] 35. 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

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

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

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

  7. 【题解】【数组】【查找】【Leetcode】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] 35. Search Insert Position 解决思路

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

  9. LeetCode 35. Search Insert Position (搜索嵌入的位置)

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

随机推荐

  1. 走向DBA[MSSQL篇] 面试官最喜欢的问题 ----索引+C#面试题客串

    原文:走向DBA[MSSQL篇] 面试官最喜欢的问题 ----索引+C#面试题客串 对大量数据进行查询时,可以应用到索引技术.索引是一种特殊类型的数据库对象,它保存着数据表中一列或者多列的排序结果,有 ...

  2. [INS-20802] Oracle Database Configuration Assistant 失败

    1.错误原因    [INS-20802] Oracle Database Configuration Assistant 失败 2.错误原因 3.解决方案 版权声明:本文博主原创文章.博客,未经同意 ...

  3. NSIS文字及字符串函数与头文件介绍

    原文 NSIS文字及字符串函数与头文件介绍 文字函数,顾名思义就是处理字符串的函数.使用这些字符串函数前,必须先包含头文件WordFunc.nsh.该头文件目前包含如下一些函数:WordFind.Wo ...

  4. j2se--Socket沟通

    第一次接触Socket课程设计大二,我在做一个图书馆管理系统.源代码是从互联网上下载,代码天天磨,隐约中记得有Socket这么一个单词. 第二次是去年代表学校參加"河北省电子信息职业技能大赛 ...

  5. hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】

    主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...

  6. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  7. git 如何让单个文件回退到指定的版本

    1.进入到文件所在文件目录,或者能找到文件的路径查看文件的修改记录 1 $ git log MainActivity.java 结果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. leetcode 之 Unique Paths

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  9. OOP 创建对象的7种方式

    JavaScript OOP 创建对象的7种方式   我写JS代码,可以说一直都是面向过程的写法,除了一些用来封装数据的对象或者jQuery插件,可以说对原生对象了解的是少之又少.所以我拿着<J ...

  10. Ubuntu下一个openldapserver部署步骤

    1:安装zlib 下载zlib-1.2.3.tar.gz(或其它版本号) wget http://down1.chinaunix.net/distfiles/zlib-1.2.3.tar.gz # . ...