题目: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. c 二维数组动态分配和释放

    c动态语言 函数声明的头文件在<stdlib.h>里 使用malloc函数为字符串分配内存 -->记得释放内存 free() #include <stdio.h> #in ...

  2. [转]浅谈Android五大布局(二)——RelativeLayout和TableLayout

    在浅谈Android五大布局(一)中已经描述了LinearLayout(线性布局).FrameLayout(单帧布局)和AbsoulteLayout(绝对布局)三种布局结构,剩下的两种布局Relati ...

  3. windows下mysql密码忘了怎么办?【转】

    前两天在windows的command命令行下,用mysqladmin导入过一些站上数据 结果悲催了,mysql.user的内容被改了,root上不了了,权限也变了. 结合了网络上的集中方法,终于成了 ...

  4. Unity-------------------------关于GUI绘制的编程

    转载:在这篇文章中我将给读者介绍Unity中的图形用户界面(GUI)编程.Unity有一个非常强大的GUI脚本API.它允许你使用脚本快速创建简单的菜单和GUI. 简介 Unity提供了使用脚本创建G ...

  5. Linux——curl(转)

    先介绍curl工具的使用,在其他文章中介绍如何使用curl发送Identity API linux curl是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具 ...

  6. Java多线程(九)之ReentrantLock与Condition

    一.ReentrantLock 类   1.1 什么是reentrantlock   java.util.concurrent.lock 中的 Lock 框架是锁定的一个抽象,它允许把锁定的实现作为 ...

  7. Jenkins使用简易教程

    Jenkins是一款能提高效率的软件,它能帮你把软件开发过程形成工作流,典型的工作流包括以下几个步骤 开发 提交 编译 测试 发布 有了Jenkins的帮助,在这5步中,除了第1步,后续的4步都是自动 ...

  8. JSP——MVC模式+Servlet生命周期

    设计模式MVC模式 模型MODEL操作数据库的增删改查——javaBean 视图VIEW显示数据——JSP 控制器CONTROLLER响应用户的——servlet Model(模型),是程序的主体部分 ...

  9. <转>Win8.1+CentOS7 双系统 U盘安装

    0.准备工作 1.宏碁 Aspire 4752G 笔记本 2.Win8.1 企业版操作系统 3.8G 以上 U 盘 4.UltraISO(当然也可以选择其他的U盘制作工具,看个人喜好) 5.下载 Ce ...

  10. hibernate.cfg配置mysql方言

    hibernate自动建表,mysql高版本不支持 type=InnoDB 中的type关键字. 应该使用engine关键字 而非type 所以需要在hibernate.cfg.xml中配置方言.否则 ...