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.

这里要考虑清楚为什么要返回left。

int findPos(int A[], int left, int right, int target) {
if (left > right) return left;
int mid = (left + right) /;
if (target > A[mid]) {
return findPos(A, mid+, right, target);
}
else if (target < A[mid]) {
return findPos(A, left, mid-, target);
}
else {
return mid;
}
} int searchInsert(int A[], int n, int target) {
if (n == ) return ;
return findPos(A, , n-, target);
}

[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: Search Insert Position 解题报告

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

  3. [LeetCode] 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 Search Insert Position Python

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

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

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

  7. LeetCode——Search Insert Position

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

  8. [Leetcode] 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 Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

随机推荐

  1. 前端之css、JavaScript和DOM

    css position 一般组合 relative+absolute,以relative为父元素,absolute依照relative进行定位. opcity: 0.5 透明度 z-index: 层 ...

  2. php 使用redis锁限制并发访问类

    1.并发访问限制问题 对于一些需要限制同一个用户并发访问的场景,如果用户并发请求多次,而服务器处理没有加锁限制,用户则可以多次请求成功. 例如换领优惠券,如果用户同一时间并发提交换领码,在没有加锁限制 ...

  3. C# 类成员备忘

    隐藏基类的方法 调用重写或隐藏的基类方法 嵌套的类型定义 隐藏基类的方法 当从基类继承一个(非抽象)成员时,也就继承了其实现的代码,如果继承的成员是虚拟的,就可以用Overrid重写这段实现代码, 无 ...

  4. Html空格字符代码:

    Html空格字符代码:  为html空格字符代码,由“&+n+b+s+p+;”组成,记住最后一个分号不要忘记了.

  5. POJ 3740

    http://poj.org/problem?id=3740 这是一道搜索+回溯的题目,也是我第一次接触到回溯. 题意就是找一些行,这些行可以使每一列都只存在一个1. 深搜加回溯: memory:11 ...

  6. MySQL ODBC for Linux

    参考自http://blog.csdn.net/allens_zhou/article/details/8575400 centos7 64bit [IP:192.168.0.100] yum ins ...

  7. C Primer Plus_第二章_C语言概述_复习题与编程练习

    REVIEW 1.如何称呼C程序的基本模块? ans 它们被称为函数 2.什么是语法错误?给出一个英语例子和一个C语言例子 me C的语法错误是指把正确的C符号放在了错误的位置 likes codin ...

  8. codeforces 581C. Developing Skills 解题报告

    题目链接:http://codeforces.com/problemset/problem/581/C 题目意思:给出 n 个数:a1, a2, ..., an (0 ≤ ai ≤ 100).给出值 ...

  9. jq获取绝对定位与相对定位的top, left值

    jquery 实现方法绝对var X = $('#ID').offset().top; var Y = $('#ID').offset().left; 相对var X = $('#ID').posit ...

  10. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...