描述: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

思路1:直观的想法,遍历数组,一旦当前元素>=target,当前元素的位置即为插入位置,边界问题处理一下即可。时间复杂度:O(n)

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

思路2:由于给定数组为已经排好序的,可以使用二分查找,比较A[mid]与target的大小。时间复杂度:O(logn)

 class Solution {
public:
int searchInsert(int A[], int n, int target) { int start = ;
int end = n - ;
int mid = ; while(end >= start) { mid = (start + end) / ;
if(target == A[mid])
return mid;
else if(target > A[mid])
start = mid + ;
else
end = mid - ;
} if(target > A[mid])
return mid + ;
else
return mid; }
};

LeetCode Problem 35:Search Insert Position的更多相关文章

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

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

  2. 【LeetCode】35. Search Insert Position (2 solutions)

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

  3. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

  4. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  5. Leetcode No.35 Search Insert Position(c++实现)

    1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...

  6. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

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

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

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

  9. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

随机推荐

  1. 阻止 gulp 发生异常后退出

    gulp-plumber 可以阻止 gulp 插件发生错误导致进程退出并输出错误日志.

  2. C中的C文件与h文件辨析(转)

    简单的说其实要理解C文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程,一般说来编译器会做以下几个过程:       1.预处理阶段 2.词法与语法分析阶段 3.编译阶段,首先编译成 ...

  3. Linq的日期比较

    在一个项目中要进行linq的日期比较,从一个表的时间类型字段中取出日期,比较是否为当天日期.语句如下: epark_middlelayerEntities eparkMiddle = new epar ...

  4. 【Java】Java_15 打印九九乘法表

    使用For循环嵌套即可打印九九乘法表 以下是具体代码: /** * 打印九九乘法表 */ package com.oliver.test; public class TestMultiplicatio ...

  5. Oracle转化成为百分比

    两种方式都行: ),)||'%' 百分比 from dual; ),'99D99')||'%' 百分比 from dual 第一种方式通过round可以自己选择精确到位数.

  6. shll 基础讲解

    http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html Shell编程基础 $# 命令行得到的参数个数 $@ 命令行得到的所有参数作 ...

  7. 用Darwin开发RTSP级联server(拉模式转发)(附源代码)

    源代码下载地址:https://github.com/EasyDarwin orwww.easydarwin.org 在博客 在Darwin进行实时视频转发的两种模式 中,我们描写叙述了流媒体serv ...

  8. Mac 上的终端神器 iTerm2

    官方下载地址:http://www.iterm2.com/ 主题下载地址:https://github.com/mbadolato/iTerm2-Color-Schemes 第三方教程推荐:http: ...

  9. 使用BMap.Label给百度地图的BMap.Marker上加上数字序号

    marker = new BMap.Marker(pointList[i]) marker.setLabel(getNumberLabel(i)); function getNumberLabel(n ...

  10. &lt;LeetCode OJ&gt; 26 / 264 / 313 Ugly Number (I / II / III)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...