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. ICMP协议Ping命令的应用

    ICMP的全称是 Internet Control Message Protocol ,它是TCP/IP协议族的一个子协议,属于网络层协议,用于在IP主机.路由器之间传递控制消息.从技术角度来讲,就是 ...

  2. 栈实现java

    栈是一种“先去后出”的抽象的数据结构.例如:我们在洗盘子的时候,洗完一个盘子,将其放在一摞盘子的最上面,但我们全部洗完后,要是有盘子时,我们会先从最上面的盘子开始使用,这种例子就像栈的数据结构一样,先 ...

  3. Linux 下开启ssh服务(转)

    二.SSH SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目 ...

  4. CKEditor上传插件

    CKEditor上传插件 前言 CKEditor上传插件是不是免费的,与您分享在此开发.这个插件是基于ASP.NET MVC下开发的,假设是webform的用户或者其他语言的用户.能够參考把serve ...

  5. C# WinForm多线程(二)ThreadPool 与 Timer

    本文接上文,继续探讨WinForm中的多线程问题,再次主要探讨threadpool 和timer 一  ThreadPool 线程池(ThreadPool)是一种相对较简单的方法,它适应于一些需要多个 ...

  6. C# WinForm多线程(一)Thread类库

    Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括 ...

  7. 如何使用junit4写单元测试用例(转)

    JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写. 先 简单解释一下什么是Annotation,这个单词一般是翻译成元数据.元数 ...

  8. [LeetCode226]Invert Binary Tree

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 反转二叉树,左右儿子值交换 代码: / ...

  9. Ubuntu Linux中开启MySQL远程访问功能

    为了给服务器一个真实的运行环境,今天在内网中部署了一台Ubuntu Linux服务器,其中最头疼的就是MySQL的安装,apt下载更新的速度太慢了,于是就自行编译了一下MySQL的源码,具体的编译方法 ...

  10. Python 基金会 —— 模块和包简介

    一.模块(Module) 1.模块的作用      在交互模式下输出的变量和函数定义,一旦终端重新启动后,这些定义就都不存在了,为了持久保存这些变量.函数等的定义,Python中引入了模块(Modul ...