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. ALSA安装编程指南

     ALSA全指南 一.什么是ALSA ALSA是Advanced Linux Sound Architecture,高级Linux声音架构的简称,它在Linux操作系统上提供了音频和MIDI(Mu ...

  2. 2014在辛星Javascript口译科

    ***************概要*************** 1.Javascript是一种原型化继承的基于对象的动态类型的脚本语言,它区分大写和小写.主要执行在client,用户即使响应用户的操 ...

  3. 自己定义actionbar

    android中的actionbar可提供自己定义view.详细是先写好自己定义view的布局,然后在代码中获取Actionbar对象.调用 setCustomView方法. 可是这样,它还是会显示前 ...

  4. 9种CSS3 blend模式制作的鼠标滑过图片标题特效

    这是一款使用CSS3 background-blend-mode制作的鼠标滑过图片标题特效.该图片标题特效在鼠标滑过一张图片的时候,图片的标题会对应的动画,而且图片会使用css blend模式渲染为很 ...

  5. 使用Simple DNS plus 构建自己的DNS

    1.下载并安装Simple DNS plus 2.界面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2tfYm9zcw==/font/5a6L ...

  6. C++ - 内置类型的最大值宏定义

    内置类型的最大值宏定义 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24311895 C++中, 常常会使用, 某些类型的最大值 ...

  7. LR杂记 - loadrunner各项指标结果分析

    Transactions (用户事务分析) 用户事务分析是站在用户角度进行的基础性能分析. 1 . Transation Sunmmary (事务综述) 对事务进行综合分析是性能分析的第一步,通过分析 ...

  8. DevExpress 12.1 换肤 超级简单的方法(2013-11-5版)

    本例子是按照DevExpress 12.1 版本 进行演示.请先准备好DevExpress.BonusSkins.v12.1.dll 和DevExpress.Utils.v12.1.dll 1.首先添 ...

  9. wpf 9张图片的连连看

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  10. 数据库管理——安全管理——识别SQLServer中空密码或者弱密码的登录名

    原文:数据库管理--安全管理--识别SQLServer中空密码或者弱密码的登录名 原文译自: http://www.mssqltips.com/sqlservertip/2775/identify-b ...