问题:链接

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. libCurl的文件上传

    最近在需要使用curl的上传功能,使用libCurl来实现.因此,先使用curl命令操作,然后再使用libCurl实现. 基于Http协议的文件上传的标准方法是: 基于POST Form的文件上传  ...

  2. Java ThreadLocal 学习

    同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式. 而ThreadLocal是隔离多个线程的数据共享,从根本上就不在多个线程之间共享资源(变量),这样当然不需要对多 ...

  3. struts2自己定义类型转换器

    1.1.  struts2自己定义类型转换器 1)        自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...

  4. BI系统的应用组织思路与数据分析模式

    BI商业智能软件一般都会提供若干数据整合.数据查询.分析与评价.数据可视化及数据分享的手段,但是在BI项目的构建与实施过程中,如果不按照一定的应用组织思路.数据分析模式及分析流程使用这些工具或手段,呈 ...

  5. jQuery的AJAX方法简介及与其他文件$符号冲突的解决办法

    一.重要的jQuery AJAX方法简介 $.load(url) 从服务器载入数据 $.get(url,callback) 从服务器请求数据,并执行回调函数 $.post(url,data,callb ...

  6. MD5算法【计算文件和字符串的MD5值】

    1. MD5算法是一种散列(hash)算法(摘要算法,指纹算法),不是一种加密算法(易错).任何长度的任意内容都可以用MD5计算出散列值.MD5的前身:MD2.MD3.MD4.介绍工具:CalcMD5 ...

  7. iOS离线打包

    预备环境 iOS开发环境,Mac OS.XCode 7.2以上版本: 下载HBuilder离线打包iOS版SDK(5+ SDK下载). SDK目录说明 HBuilder-Hello:离线打包演示应用: ...

  8. php随笔6-thinkphp OA系统 JS 实时显示当前时间

    不多说,直入主题: JS. // JavaScript Document function showtime() { var today,hour,second,minute,year,month,d ...

  9. 轮播组件iceSlider

    ~~~~作为编写组件的一个参考吧,在js输出组件样式的问题上 探讨一下 尽量简化组件的调用 function iceSlider(element,options) { /* 功能:广告翻转切换控制 参 ...

  10. Failed to load the JNI shared library

    解决Eclipse无法打开"Failed to load the JNI shared library" 这是由于JDK配置错误所导致的现象. 一般说来,新购笔记本会预装64位的w ...