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. HDU 2222 ----AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  2. zencart安装后修改configure.php权限

    刚安装完zencart的时候,打开前台页面会遇到下面的安全提示: “警告: 配置文件允许写入: E:\upupw\htdocs\mysite\includes\configure.php,存在安全隐患 ...

  3. css position属性

    absolute, 屏幕为参照, 但固定在页面上,随页面滚动而动. fixed, 父元素没有指定position属性(或position属性为static时)==>屏幕为参照,固定在屏幕的某个位 ...

  4. n全排列输出和 n个数的组合(数字范围a~b)

    n全排列输出: int WPermutation(int num, bool bRepeat) num表示num全排列 bRepeat标志是否产生重复元素的序列. int Permutation(in ...

  5. 页面遮罩层,并且阻止页面body滚动。bootstrap模态框原理

    实现思路: 1.需要有一个层将body遮住,放在body上方. 2.修改body的overflow属性值为:hidden 废话不多说了,将关键代码贴出来了,兼容火狐,谷歌,ie 遮罩层的样式代码,红色 ...

  6. 【GoLang】GoLang for 中有多个循环变量怎么处理?

    代码示例: sum := , ; i <= && j <= ; i, j = i+, j- { t.Log("i: ", i) t.Log(" ...

  7. 套接字IO超时设置和使用select实现超时管理

    在涉及套接字IO超时的设置上有一下3种方法: 1.调用alarm,它在指定的时期满时产生SIGALRM信号.这个方法涉及信号的处理,而信号处理在不同的实现上存在差异,而且可能干扰进程中现有的alarm ...

  8. jsp自定义标签(时间格式化包括Long转时间)

    1.jsp自带标签的格式化: jstl fmt 函数大全:主要针对格式化功能 Tags   fmt:requestEncoding fmt:setLocale fmt:timeZone fmt:set ...

  9. hiho一下第二周 Trie树

    题目链接:http://hihocoder.com/problemset/problem/1014 #include <iostream> #include <cstdio> ...

  10. C# 对象深度拷贝

    转载 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using ...