题意

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. Linux 硬盘格式化、分区、挂载、卸载、删除分区,Linux重新调整分区

    目录 Linux 硬盘格式化.分区.挂载.卸载.删除分区 0. 查看挂载情况 1. 查看硬盘信息 2. 创建分区 3. 查看磁盘信息 4. 格式化分区 5. 将分区信息写入fstab, 设置开机自动挂 ...

  2. Pyhon环境变量的一些坑

    在正常的情况下,使用编译器执行Python文件,无需考虑环境变量的改变 例:sum --one --one1.py --two --two1.py 在执行one.py文件需要调用 two.py中某个方 ...

  3. python中urllib的整理

    本不想使用这个玩意,奈何看到很多地方使用,随手整理下 urllib模块提供的urlretrieve()函数,urlretrieve()方法直接将远程数据下载到本地 urlretrieve(url, f ...

  4. SVN与Git比较的优缺点差异

    目录: SVN与Git比较(一)集中式vs分布式 SVN与Git比较(二)版本库与工作区 SVN与Git比较(三)全局版本号和全球版本号 SVN与Git比较(四)部分检出 SVN与Git比较(五)更新 ...

  5. MySQL面试题之为什么要为innodb表设置自增列做主键?

    为什么要为innodb表设置自增列做主键? 1.使用自增列做主键,写入顺序是自增的,和B+数叶子节点分裂顺序一致 2.表不指定自增列做主键,同时也没有可以被选为主键的唯一索引,InnoDB就会选择内置 ...

  6. python之字符串的常用操作(转)

    1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + str2) >> ...

  7. Linux 小知识翻译 - 「packet」(网络数据包)

    用手机接收邮件或者访问网页的时候,一般会说有「packet费用」(这是日本的说法,在中国好像一般都说 “流量费”),即使对网络不太熟悉的人也知道「packet」这个词(这里也是日本的情况). 那么,「 ...

  8. windows 10 安装TortoiseSVN.msi时报2503的错误

    解释: 出现这个错误的原因是权限不足,右击安装程序也没有“以管理员身份运行”按钮. 解决: 输入 msiexec /package  F:\TortoiseSVN-1.9.7.27907-x64.ms ...

  9. c++11の异步方法 及线程间通信

    1. std::promise 可以用来在线程间提供数据传递. std::future = std::promise.get_future(). 线程中可以对promise赋值std::promise ...

  10. Graphviz安装及简单使用

    Graphviz Windows环境安装: 1.官网下载 官网下载地址 2.配置环境变量 将graphviz安装目录下的bin文件夹添加到Path环境变量中: 3.验证是否安装并配置成功 进入wind ...