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

解题思路:

典型的二分查找应用,注意数组下标为0~n-1,但是可能数字需要插在数组最后第n的位置上。

由于需要找到一个大于等于target数所在的位置,进行插入;所以当nums[mid] > target时,r应该等于mid;因此采用 mid=(l+r)/2,l=mid+1的组合;

对于二分查找有兴趣的同学,可以看另一道Leetcode题目Search for a Range,对二分查找的应用更加全面,在里面,我也写了一些自己学习二分查找总结的心得;

代码:

 class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int l = ;
int r = nums.size();
while (l < r) {
int mid = (l + r) / ;
if (nums[mid] > target)
r = mid;
else if (nums[mid] == target)
return mid;
else
l = mid + ;
}
return r;
}
};

【Leetcode】【Medium】Search Insert Position的更多相关文章

  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】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. 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题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  6. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

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

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

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

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

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

  10. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

随机推荐

  1. lucene原理

    lucene查找原理: https://yq.aliyun.com/articles/581877

  2. c# 如何得到一个字符的ASCII码

    '; int b = (int)a; 就这么简单..

  3. vue与TypeScript集成配置最简教程

    https://blog.csdn.net/u014633852/article/details/73706459 https://segmentfault.com/a/119000001187808 ...

  4. openerp学习笔记 对象继承,对象初始化数据

    1.对象继承     _inherit = "product.product" 继承产品对象,给产品对象添加字段或方法,不需要设置 _name._table 等属性     注意: ...

  5. (转)nginx日志配置指令详解

    这篇文章主要介绍了nginx日志配置指令详解,nginx有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志,需要的朋友可以参考下日志对于统计排错来说非常有利的.本文总结了nginx日 ...

  6. 理解session和cookie

    Session 与 Cookie 的作用都是为了保持访问用户与后端服务器的交互状态.它们有各自的优点,也有各自的缺陷,然而具有讽刺意味的是它们的优点和它们的使用场景又是矛盾的.例如,使用 Cookie ...

  7. orcale 之 多表查询

    在以往的工作中我们不可能单一的从一张表中查询数据,而在开始设计数据库的时候,我们会把一些数据存放在不同的数据表中,因此往往需要从多个数据表中获取到我们想要的数据. 笛卡儿积 在学习这些之前我们先了解一 ...

  8. Linux 命令学习之ls

    ls(list) 功能说明: ls 命令是Linux中使用最频繁的命令,即list的缩写,默认情况下会罗列出当前文件下的所有文件.同时ls 也可以指定罗列某个文件下的文件.而且该命令可以查看文件的一些 ...

  9. oracle timestamp转换date及date类型相减

    --timestamp转换为date(ts字段为timestamp类型) ; --timestamp转换为date(ts字段为timestamp类型) ; --date相减 )) FROM dual; ...

  10. CCF ISBN号码 201312-2

    ISBN号码 问题描述 试题编号: 201312-2 试题名称: ISBN号码 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 每一本正式出版的图书都有一个ISBN号码与之对应 ...