题目: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++复合类型(数组)

    1.数组 数组之所以被称为复合类型, 是因为它是使用其他类型来创建的 例如: short months[12]: 那么格式为 typename arrayname [arraysize]  注意:ar ...

  2. Linux命令_磁盘管理_查看磁盘或目录的容量

    软件环境:虚拟机VM12,Linux版本 CentOS 7.3 命令 df (disk filesystem) 用于查看已挂载磁盘的总容量.使用容量.剩余容量等,可以不加任何参数,默认以KB为单位显示 ...

  3. windows版mysql配置--my.ini

    完整配置如下: # power by phpStudy 2014 www.phpStudy.net 官网下载最新版 [client] port=3306 [mysql] [mysqld] port=3 ...

  4. utf8_unicode_ci与utf8_general_ci的区别

    From: http://www.chinaz.com/program/2010/0225/107151.shtml 当前,utf8_unicode_ci校对规则仅部分支持Unicode校对规则算法. ...

  5. 浪漫程序员 HTML5爱心表白动画

    我们程序员在追求爱情方面也是非常浪漫的,下面是一位同学利用自己所学的HTML5知识自制的HTML5爱心表白动画,画面非常温馨甜蜜,这样的创意很容易打动女孩,如果你是单身的程序员,也赶紧来制作自己的爱心 ...

  6. YII2 搭建redis拓展(教程)

    安装redis扩展: 1.通过composer进行安装,到项目根目录cmd运行(推荐) php composer.phar require --prefer-dist yiisoft/yii2-red ...

  7. 使用 StoryBoard 实现左右按钮切换图片的浏览效果

    关键技能:使用故事板进行布局时,点击选中控件(组件)并按住 control 键向某个方向拖动,产生一条实线,然后弹出的窗口可以设置控件(组件)的布局约束条件:从而实现自动布局 AutoLayout 效 ...

  8. 图解HTTP学习笔记——确认访问用户身份的认证

    前言 认证功能能让Web页面只被有权限的人访问.而认证机制究竟是怎样一个原理呢?通过今天的学习能对这个有个大致的了解. 正文 什么是认证 计算机无法判断对方的身份,需要客户端自报家门. 服务端为确认客 ...

  9. 各大公司Java面试题超详细总结

    ThreadLocal(线程变量副本)Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量.采用空间换时间,它用于线程间的数据隔离,为每一个使用该变量的线程提供一个副 ...

  10. 用Eclipse编写Android程序的代码提示功能

    用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示     Window->Preferences- ...