问题:链接

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(A[0] >= target || n == 0)
return 0;
for(int i = 0; i < n; i++)
{
if(A[i] == target)
return i;
else if(i == n-1)
return n;
else if(A[i] < target && A[i+1] > target)
{
return i+1;
}
}
}
};

Search Insert Position--寻找插入点Given a sorted array and a target value, return the index if the target的更多相关文章

  1. [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 ...

  2. [Leetcode] search insert position 寻找插入位置

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

  3. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

  4. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  5. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

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

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

  7. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  8. Leetcode35 Search Insert Position 解题思路(python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...

  9. leetcode-algorithms-35 Search Insert Position

    leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...

随机推荐

  1. Linux批量重命名文件

    五种方法实现Linux批量重命名文件 Linux批量重命名文件是指对某些特定的文件统一进行重新命名,以改变原来一批文件的名称,这里介绍五种方法来实现. Linux批量重命名文件会涉及到改变一个字母.改 ...

  2. Adobe Flash Player已经终止一项可能不安全的操作,解决方案

    http://www.macromedia.com/support/documentation/cn/flashplayer/help/settings_manager04.html

  3. 前端性能监控系统ShowSlow

    作者:zhanhailiang 日期:2014-11-14 1. 简单介绍 ShowSlow是开源的前端性能监控系统,提供了下面功能: 前端性能指标数据收集功能:ShowSlow原生提供了数据收集工具 ...

  4. oracle 10 g 需要启动的2个服务

    开始-> run -> cmd -> services.msc OracleOraDB10g_home1TNSListener OracleServiceORCL

  5. Html.ActionLink(转载)

    @Html.ActionLink 代码: <h2>HtmlHelper</h2>@Html.ActionLink("默认","Index" ...

  6. php随笔3-thinkphp 学习-ThinkPHP3.1快速入门(1)基础

    ThinkPHP3.1快速入门(1)基础 简介 ThinkPHP是一个快速.简单的基于MVC和面向对象的轻量级PHP开发 框架,遵循Apache2开源协议发布,从诞生以来一直秉承简洁实用的设计原则,在 ...

  7. 初识 python

    Python 语言介绍 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python变化 python 2 和 python 3 1.1/2 等于0.5 2.print ...

  8. Cobbler自动化部署

    一:PXE.Kickstart与Cobbler的概念: PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的技术,需要网卡的硬件支持,工作于C/S的 ...

  9. Python之路:Python 函数

    一.函数式编程:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装 二. 函数的定义和使用 def 函数名(参数): ... 函数体 ... 函数的定义主要有 ...

  10. Redhat 5.8系统安装R语言作Arima模型预测

    请见Github博客:http://wuxichen.github.io/Myblog/timeseries/2014/09/02/RJavaonLinux.html