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. epoll中epoll_data_t 中fd和ptr的用法

    https://blog.csdn.net/u011123091/article/details/81867078 Linux高性能服务器P152

  2. Java 覆写初探

    Java 覆写 继承性的主要特征是子类可以根据父类已有的功能进行功能扩展,但是在子类定义属性或方法的时候有可能定义属性和方法和父类同名,在此类情况下就称为:“覆写”. 方法的覆写:[改良原本功能不足的 ...

  3. Java 数组实现堆栈操作

    class Stack { private int stck[] ; private int tos ; Stack(int size) { // 一个参数的构造参数 stck = new int[s ...

  4. docker编排工具,docker-compose下载与安装

    安装很简单,但是难免会遇到问题:1.安装curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compos ...

  5. jquery jgrid filterToolBar beforeSearch 修改postData

    beforeSearch: function() { var posted_data = $("#mygrid").jqGrid('getGridParam,'postData') ...

  6. Go的接口总结

    一.什么是接口 接口类型是一种抽象的类型,它描述了一系列方法的集合. 接口约定:接口类型中定义的方法即为约定,若一个具体类型实现了所有这些方法,则该类型就满足该接口的约定,或者说它是这个接口类型的实例 ...

  7. SQL 工具系列一

    1.误删除数据恢复篇 ApexSQL Recover   可以恢复Delete Truncate  drop,恢复 二进制大型对象 测试版本  每10行才会恢复 评估版本下载地址:只能用14天 所以基 ...

  8. Android 屏蔽recent task 按钮

    Step 1 Add this permission to the manifest.xml file <uses-permission android:name="android.p ...

  9. efcore 配置链接sqlserver

    本文将在asp.net core api 项目中使用efcore corefirst模式 简单配置链接sqlserver数据库,以及简单的数据库迁移操作 一 新建项目 1. 首先我们先用vs2017 ...

  10. jmeter简单使用示例

    1.下载后解压,运行bin目录下的jmeter.bat 2.add ThreadGroup 3.add request 4.add listener