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

二分查找。一点小的差别就是当数组不含目标数字时,返回应该插入的位置。

AC code:

class Solution {
public:
int searchInsert(int A[], int n, int target)
{
int begin=0,end=n-1,mid=0;
while(begin<=end)
{
mid=(begin+end)/2;
if(A[mid]==target)
return mid;
if(A[mid]>target)
end=mid-1;
else
begin=mid+1;
}
if(A[mid]>target)
return mid;
return mid+1;
}
};

版权声明:本文博主原创文章,博客,未经同意不得转载。

leetcode 刷道题 70 earch Insert Position 二进制搜索插入位置的更多相关文章

  1. [LC]35题 Search Insert Position (搜索插入位置)

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

  2. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  3. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  4. Leecode刷题之旅-C语言/python-35.搜索插入位置

    /* * @lc app=leetcode.cn id=35 lang=c * * [35] 搜索插入位置 * * https://leetcode-cn.com/problems/search-in ...

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

  6. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

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

  8. LeetCode记录之35——Search Insert Position

    这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...

  9. 【leetcode刷题笔记】Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

随机推荐

  1. Test SRM Level Two: CountExpressions, Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=8157 这道题目跟扑克牌算24的题目比较像,但要简单一些.点击查 ...

  2. Embedding Lua, in Scala, using Java(转)

    LuaJ I was reading over the list of features that CurioDB lacks compared to Redis , that I’d previou ...

  3. php定时运行任务(windows7)

    1:自己写的php档,下面是我的一些php档 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzE2MDAyNA==/font/5a6L5L2T/f ...

  4. RabbitMQ与java、Spring结合实例详细讲解(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了rabbitMq,提供了如何在Ubuntu下安装RabbitMQ 服务的方法. ...

  5. 同一个PC只能运行一个应用实例(考虑多个用户会话情况)

    原文:同一个PC只能运行一个应用实例(考虑多个用户会话情况) class Program { private static Mutex m; [STAThread] static void Main( ...

  6. synchronized与static synchronized 差异

    1.synchronized与static synchronized 差异       synchronized是对类的当前实例进行加锁,防止其它线程同一时候訪问该类的该实例的全部synchroniz ...

  7. POJ2029——Get Many Persimmon Trees

    Get Many Persimmon Trees Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3656   Accepte ...

  8. Android SharedPreferences复杂的存储

    我们知道SharedPreferences简单类型的数据.比如.String.int等. 假设想用SharedPreferences存取更复杂的数据类型(类.图像等),就须要对这些数据进行编码. 我们 ...

  9. 2014年度辛星全然解读html第七节

    经过前面六节的学习,我们大致清楚了HTML教程中的基础内容,那么接下来我们開始继续向后推进,能够说,以下我们介绍一下HTML中的区块. ***************区块*************** ...

  10. 终端查询数据库sqlite(创建你自己,或者是coredata创建)那里的东西

    首先需要知道数据库的路径,coredata一般都是在沙箱中创建Documents目录的.虽然他建立了自己看, sqlite3 #数据库路径#        //进入数据库 .tables  //查看数 ...