题意

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插入到下一个位置。

class Solution
{
public:
int searchInsert(vector<int>& nums, int target)
{
int left = 0;
int right = nums.size() - 1;
int mid = 0;
int index = 0; while(left <= right)
{
mid = (left + right) >> 1;
if(nums[mid] == target)
{
index = mid;
break;
}
else
if(nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
if(nums[index] == target)
return index;
if(target > nums[mid])
return mid + 1;
else
return mid;
}
};

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. Video.js web视频播放器

    免费视频播放器videojs中文教程 Video.js是一款web视频播放器,支持html5和flash两种播放方式.更多关于video.js的介绍,可以访问官方网站介绍,我之前也写过一篇关于vide ...

  2. JavaScript 中的匿名函数((function() {})();)与变量的作用域

    以前都是直接用前端框架Bootstrap,突然想看看Javascript,发现javascript是个非常有趣的东西,这里把刚碰到的一个小问题的理解做下笔录(废话不多说,上代码). /** * Exa ...

  3. win7系统开机后电脑桌面背景变黑的解决方法

    自从微软放弃了对win7系统的维护更新,一些BUG也就慢慢出现了,最近用户反映,开机后电脑桌面背景变黑,即使重新换了桌面背景,还是会出现这种情况.下面小编就来告诉大家怎样解决这一问题. 1.点击开始菜 ...

  4. 05LaTeX学习系列之---TeX的命令行操作

    目录 目录 前言 (一)查看版本号 1.查看TeX的版本号 2.查看LaTeX的版本号 3.查看XeLeTeX的版本号 (二)更行版本 (三)用命令行来编译.tex文件 1.用LaTeX编译 2.用X ...

  5. Python如何输出带颜色的文字方法

    我们在使用python运维与开发的过程中,经常需要打印显示各种信息.海量的信息堆砌在控制台中,就会导致各种信息都显示在一起,降低了重要信息的可读性.这时候,如果能给重要的信息加上差异的字体颜色,那么就 ...

  6. 写jQuery插件该注意的

    写好jQuery插件,有一些注意的地方(持续添加). 支持UMD 现在前端开发讲究模块化,所以jQuery插件也最好能够兼顾模块化. 模块化模式大概有几种: AMD.CommonJs.UMD. AMD ...

  7. Alpha冲刺! Day12 - 砍柴

    Alpha冲刺! Day12 - 砍柴 今日已完成 晨瑶:终于更了 Gitkraken 团队协作教程. 昭锡:初步学习了解Android动画. 永盛:用户逻辑基本完成. 立强:从众多开源库中找到两个合 ...

  8. 使用python制作时间戳转换工具

    使用python制作时间戳转换工具 python 时间戳转日期 日期转时间戳 前言:作为一个程序员一般情况下,json和时间戳是常用的两个工具,我咨询过很多个朋友,他们一般都是通过在线工具对json进 ...

  9. matlab slice

    前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...

  10. Objective-C Mach-O文件格式深入理解

    Mach-O(Mach Object),是一种基于Mach内核的文件格式,苹果很多文件都采用这种格式,最常见的就是可执行文件和动态库. 当然,还有.o的目标文件..a和.framework的静态库以及 ...