本文为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. Divide Two Integers leetcode java

    题目: Divide two integers without using multiplication, division and mod operator. 题解: 这道题我自己没想出来...乘除 ...

  2. Builder 建造者模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. 从javascript读取cookies说开去:谈谈网页的本地化存储

    学习要点:1.cookies 2.cookies 局限性 3.其他存储 随着 Web 越来越复杂,开发者急切的需要能够本地化存储的脚本功能.这个时候,第一个出现的方案:cookie 诞生了.cooki ...

  4. Android -- Drawable && Bitmap

    Bitmap转Drawable Bitmap bm=xxx; BitmapDrawable bd=new BitmapDrawable(bm); 因为BtimapDrawable是Drawable的子 ...

  5. MongoDB学习笔记(一)--基础

    Insert                                                                                        MongoD ...

  6. 下载RAD

    1.登录https://w3-103.ibm.com/software/xl/download/ticket.do 2.输入Intranet ID和pswd,然后选I Agree. 3.然后点Sear ...

  7. TCP/IP、SOCKET、HTTP之间的联系与区别

    主要内容: 1.网络的七层协议 2.TCP/IP.SOCKET.HTTP简介 3.TCP连接.HTTP连接.Socket连接的区别 一.网络的七层协议 网络七层由下往上分别为物理层.数据链路层.网络层 ...

  8. 缺少dll文件的解决方法

    1.什么是dll文件 从专业的角度来说,dll文件,即动态连接库,是一种不可执行的二进制文件,它允许程序共享执行特殊任务所必需的代码和其他资源.打个比方,相当于你去饭店吃饭,只人带上钱或卡就可以了,不 ...

  9. GLSL语言内置的变量详解

    GLSL语言内置的变量,包括内置的顶点属性(attribute).一致变量(uniform).易变变量(varying)以及常量(const),一方面加深印象,另一方面今天的文章可以为以后的编程做查询 ...

  10. 使用ADS1.2的注意事项及常用技巧

    如果创建的项目中有多个文件时(尤其是编译后的镜像大小超过4K时),一定要在link order栏下调整文件顺序,主要是前几个文件的顺序(2440init.s.2440slib.s.nand.c这三个文 ...