题意

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

给定一个有序数组和一个目标值,查找出目标值在数组中出现的位置,如果没有出现,则返回它应该插入的位置。

解法

二分查找,记录mid值,查找结束后判断nums[mid] == target,如果相等的话就返回,不相等的话判断这个位置上的值是不是比target大,如果比它大那么target就直接插在mid这个位置,比target小的话就把target插入到下一个位置。

  1. class Solution
  2. {
  3. public:
  4. int searchInsert(vector<int>& nums, int target)
  5. {
  6. int left = 0;
  7. int right = nums.size() - 1;
  8. int mid = 0;
  9. int index = 0;
  10. while(left <= right)
  11. {
  12. mid = (left + right) >> 1;
  13. if(nums[mid] == target)
  14. {
  15. index = mid;
  16. break;
  17. }
  18. else
  19. if(nums[mid] < target)
  20. left = mid + 1;
  21. else
  22. right = mid - 1;
  23. }
  24. if(nums[index] == target)
  25. return index;
  26. if(target > nums[mid])
  27. return mid + 1;
  28. else
  29. return mid;
  30. }
  31. };

LeetCode Search Insert Position (二分查找)的更多相关文章

  1. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

  2. 35. Search Insert Position(二分查找)

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

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

  4. LeetCode: Search Insert Position 解题报告

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

  5. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

  6. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  7. 【LeetCode每天一题】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】- Search Insert Position(查找插入的位置)

    [ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...

  9. [Leetcode] search insert position 寻找插入位置

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

随机推荐

  1. 根据学习廖雪峰老师的git教程做的笔记

    根据学习廖雪峰老师的git教程做的笔记 安装git 进行git的配置 配置您的用户名和邮箱地址,使用--global 这个参数表明了在此台机器上的所有仓库都会使用该配置 $ git config -- ...

  2. SQL Server 将一个表中字段的值复制到另一个表的字段中

    具体方法如下 一:update 表2 set (要插入的列名)= select 表1.某一列 from 表1 left jion 表2 on 表1和表2的关联 where ..... 二:update ...

  3. ALSA声卡驱动的DAPM(二)-建立过程

    在上一篇文章中,我们重点介绍了widget.path.route之间的关系及其widget的注册: http://www.cnblogs.com/linhaostudy/p/8509899.html ...

  4. 建立标准编码规则(一)-自定义C#代码分析器

    1.下载Roslyn的Visual Studio分析器模板插件(VS2015 或VS2017) https://marketplace.visualstudio.com/items?itemName= ...

  5. CF848C:Goodbye Souvenir(CDQ分治)

    Description 给定长度为$n$的数组, 定义数字$X$在$[l,r]$内的值为数字$X$在$[l,r]$内最后一次出现位置的下标减去第一次出现位置的下标给定$m$次询问, 每次询问有三个整数 ...

  6. Eclipse中安装springmvc插件

    我网上找了很多方法,常见的两种: 方法一: 先去下载spring-framework-x.x.x.RELEASE.zip包,然后解压,后面需要配置什么,具体的笔者也记不了,哈哈哈 方法二: 打开菜单栏 ...

  7. Note 387206.1 ORA-15041: ASM diskgroup with unlike disks sizes

    Applies to: Oracle Server - Enterprise Edition - Version: 10.1.0.2 to 10.2.0.1 - Release: 10.1 to 10 ...

  8. Excel里面Left这个怎么用?

    LEFT 返回文本字符串中第一个字符或前几个字符LEFT(A2, 4)LEFT 返回前 4 个字符,因为每个字符按 1 计数.无论您计算机上的默认语言设置如何,函数 LEFT 都返回前 4 个字符.

  9. QT 12 QTimage转换Mat

    QImage cvMat2QImage(const cv::Mat& mat) { // 8-bits unsigned, NO. OF CHANNELS = 1 if(mat.type() ...

  10. mybatis基础系列(一)——mybatis入门

    好久不发博客了,写博文的一个好处是能让心静下来,整理下之前学习过的一些知识一起分享,大神路过~ mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. ...