题目: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

代码:

 class Solution
{
public:
int searchInsert(int A[], int n, int target)
{
if (target < A[])
return ;
if (target>A[n-])
return n;
for (int i = ; i < n; ++i)
{
if (A[i] == target)
return i;
else if (A[i]<target&&A[i+]>target)
return i+;
}
return ;
}
};

【LeetCode OJ】Search Insert Position的更多相关文章

  1. LeetCode OJ 35. Search Insert Position

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

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

  3. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  4. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

  5. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  6. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  7. 【Leetcode】【Medium】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】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

    这是悦乐书的第152次更新,第154篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第11题(顺位题号是35).给定排序数组和目标值,如果找到目标,则返回索引. 如果没有, ...

随机推荐

  1. python一天一题(1)

    #有一个文件,文件名为output_1981.10.21.txt . # 下面使用Python: 读取文件名中的日期时间信息,并找出这一天是周几. # 将文件改名为output_YYYY-MM-DD- ...

  2. Maven构建自动化

    构建自动化定义相关工程项目构建过程中,在当项目构建成功完成启动的情况下,来确保所依赖的项目是稳定的. 实例 考虑一个团队正在开发一个项目总线核心API上的其他两个项目的应用程序:网页UI和应用程序的桌 ...

  3. 一个类似于postman的协议测试工具

    协议测试工具使用postman相当便捷,不过有一个问题,就是每个人都要装一个这个东西,并且测试文件导来导去,还是觉得麻烦了点. 最重要的是postman不能修改,有一些定制功能postman明显力不从 ...

  4. 嵌入式开发之hi3519--- pcie dma和dma cache 缓存更新sync memery

    http://blog.csdn.net/likeping/article/details/42235111 linux下dma 管理 http://blog.csdn.net/skyflying20 ...

  5. 开源轻量级分布式文件系统--FastDFS

    FastDFS一个高效的分布式文件系统 分布式文件系统FastDFS原理介绍 分布式文件系统FastDFS设计原理 FastDFS安装.配置.部署(一)-安装和部署 分布式文件系统 - FastDFS ...

  6. Ubuntu+Eclipse+SVN 版本控制配置笔记

    第一步:先更新系统内部软件包缓存(预防出错) #  sudo dpkg --clear-avail #  sudo apt-get update 第二步:安装Eclipse的SVN接口组件“javaH ...

  7. (实用)将wordpad添加到Windows PowerShell中

    PowerShell能够直接打开notepad,但是无法调用wordpad,因为后者的可执行文件并不在系统默认的环境变量$env:Path中,只要将wordpad所在的路径添加到$env:Path,就 ...

  8. CentOS 7下彻底卸载MySQL数据库

    转载: https://zhangzifan.com/centos-7-remove-mysql.html

  9. xshell使用xftp传输文件 使用pure-ftpd搭建ftp服务

    xshell使用xftp传输文件 下载xftp5 https://www.baidu.com/link?url=8rtxgX3JRIbUFO1Samzv5aXhfwRG7Cf8i4vi573QexoH ...

  10. Linux 查看系统硬件信息

    linux查看系统的硬件信息,并不像windows那么直观,这里我罗列了查看系统信息的实用命令,并做了分类,实例解说. cpu lscpu命令,查看的是cpu的统计信息. blue@blue-pc:~ ...