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.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0
想法:使用二分查找法,找出target应该在的位置。
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
         == nums.size()){
            ;
        }
        ))
            ;
        if(target > nums.back()){
            return nums.size();
        }
        ;
        ;
        ){
            ;
            if(nums.at(mid) == target)
                return mid;
            if(nums.at(mid) < target){
                left = mid;
            }else{
                right = mid;
            }
        }
        return right;
    }
};

leetcode25—Search Insert Position的更多相关文章

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

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

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

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

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

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

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

  5. 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导致的溢 ...

  6. leetcode-algorithms-35 Search Insert Position

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

  7. LeetCode: Search Insert Position 解题报告

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

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

  9. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

随机推荐

  1. NIO学习笔记七:Pipe

    Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 这里是Pipe原理的图示: 示例代码 Pipe ...

  2. POJ3090(SummerTrainingDay04-M 欧拉函数)

    Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7450   Accepted: ...

  3. php中parse_url函数解析

    1.在php开发过程中我们经常要用到用户上传文件这个功能,那么用户上传文件我们肯定要知道用户上传文件的合法性,那么我们就要从url中获取文件的扩展名.那么就会用到parse_url()这个函数. pa ...

  4. AJAX通过HTML请求C#一般处理程序

    AJAX通过HTML请求C#一般处理程序 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"&g ...

  5. PHP 类文件的自动加载机制 __autoload()

    如果一个类在多个脚本中都需要使用,可以将一个类的定义代码,单独的封装到一个文件中,这种文件也叫作类文件,在需要的时候,将整个文件载入进来即可! PHP在执行的时候,如果发现需要一个类(只要是和这个类相 ...

  6. a 链接点击下载

    1. 将链接设置为.zip 结尾2.在a元素中添加download 属性,(目前只有chrome.firefox和opera支持) function download(src) { var $a = ...

  7. 在Ubuntu 14.04 LTS系统中设置Apache虚拟主机(一IP多访问)

    参考资料:http://os.51cto.com/art/201406/441909.htm

  8. Available to Promise (ATP) in SAP-SD

    One short note before we start off the subject: Availability refers to the projections of future mat ...

  9. 如何使用Ubuntu中的avdManager命令行为“System-Image;Androd-27;GoogleAPI;x86”创建一个AVD?

    命令是正确的,但问题是在你的系统中,你没有“android-27”操作系统. 请检查你的系统中有哪个操作系统,进入下面的目录 目录路径:-~\Android\SDK\Platform 如果没有,请先下 ...

  10. selenium 校验文件下载成功

    转自: http://www.seleniumeasy.com/selenium-tutorials/verify-file-after-downloading-using-webdriver-jav ...