本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Search Insert Position

Total Accepted: 14279 Total
Submissions: 41575

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

题意:输出一个元素在一个已排序的数组中的位置,假设不存在输出它应该插入的位置

思路:二分查找,假设找到就输出位置。找不到就输出它应该插入的位置

复杂度:时间O(log n),空间O(1)

相关题目:

Search a 2D Matrix

class Solution {
public:
    int searchInsert(int A[], int n, int target) {
    int s = 0, e = n , m ; //中间数,偶数个的时候取前一个
    while(s < e){
    m = s + (e - s) / 2;
    if(A[m] == target) return m;
    if(A[m] < target) s = m + 1;
    if(A[m] > target) e = m;
    }
    if(A[m] > target) return m;
    else return m + 1;
    }
};

Leetcode 二分查找 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 二分查找 Search in Rotated Sorted ArrayII

    Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...

  4. leetcode 二分查找 Search in Rotated Sorted Array

    Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...

  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. LeetCode Problem 35:Search Insert Position

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

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

随机推荐

  1. Map实现java缓存机制的简单实例

    缓存是Java中主要的内容,主要目的是缓解项目访问数据库的压力以及提升访问数据的效率,以下是通过Map实现java缓存的功能,并没有用cache相关框架. 一.缓存管理类 CacheMgr.java ...

  2. 我对REST的理解

    1:rest的由来 REST即表述性状态传递(英文:Representational State Transfer,简称REST) 通俗点说:资源在网络中以某种表现形式进行状态转移. 源于REST之父 ...

  3. easyui form validate总是返回false原因

    最近做表单验证用了easyui form组件.又一次发现在测试表单都填写正确了但是调试表单的代码监测到调用form的"validate"方法总是返回false 最后查了一下原因在h ...

  4. Linux-cpu分析-vmstat

    转载:https://blog.csdn.net/ty_hf/article/details/63394960 一. 前言 为了更方便的理解本篇内容含义,所以请最好看看如下繁琐的概念,更容易理解. 没 ...

  5. ECharts演习(一)

    前几天小组讨论,窗外的麻雀在电线杆上多嘴,想想很有夏天的感觉,手中的铅笔在纸上来了又回,我用几行字形容孰是孰非......... Echarts使用指南 百度网站:http://echarts.bai ...

  6. 设计好玩的3D文字效果

    ​在线演示 本地下载 好玩的3D文字效果,可以替换汉字.快来试试吧!

  7. Android之新建项目

    最近开始接触Android,实践出真理,接下来实际创建Android应用程序. 1.启动Eclipse,依次选择 " File/New/Project... " 或 " ...

  8. java.io.IOException: Input/output error

    java.io.FileOutputStream.close0(Native Method)at java.io.FileOutputStream.close(FileOutputStream.jav ...

  9. RPM 命令大全

    RPM 大全RPM 有五种基本的操作方式(不包括创建软件包): 安装, 卸载, 升级, 查询,和验证. 下面我们就来逐一的讲解吧. 一. 安装RPM包 RPM 软件包通常具有类似foo-1.0-1.i ...

  10. 有间距的表格布局 table布局

    1.先看有间距的布局效果: 2.少说多做,直接看代码(代码中有注释) <!DOCTYPE html> <html lang="zh"> <head&g ...