题意

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. Asp.Net配置不允许通过url方式访问目录下的资源

    Asp.Net网站发布后,有部分文件为了安全性,是不能直接通过url访问获取 通常有2种做法: 1.将文件目录建立在 App_code 或者App_Data 等默认的隐藏目录下 2.将文件的目录添加到 ...

  2. EasyUI报错 $(...).accordion is not a function

    参考资料: https://stackoverflow.com/questions/9017634/accordion-is-not-a-function 原因:加载了2次jquery js文件

  3. Cockpit subscriptions on CentOS 7 - This system is not registered with an entitlement server. You can use subscription-manager to register.

    下午安装 cockpit 时,使用 yum 工具的时候哦,出现如下信息: This system is not registered with an entitlement server. You c ...

  4. SAP中的ALE, IDOC

    ALE技术:应用链接支持(Application Link Enabling 简称ALE),是一项用于创建和运行分布式应用的技术.ALE是SAP的专有技术. ALE对象——ALE包含了可控的数据消息交 ...

  5. cf C. Finite or not? 数论

    You are given several queries. Each query consists of three integers pp, qq and bb. You need to answ ...

  6. dp 动态规划 之C - Apple Catching 简单基础

    终于开始写dp了,还很不熟练 It is a little known fact that cows love apples. Farmer John has two apple trees (whi ...

  7. 【BZOJ3529】数表

    数表 Description 有一张 n*m 的数表,其第i行第j列(1<=i<=n,1<=j<=m)的数值为能同时整除 i和j的所有自然数之和.给定a,计算数表中不大于a的数 ...

  8. 【洛谷】【模拟+栈】P4711 「化学」相对分子质量

    [题目传送门:] [戳] (https://www.luogu.org/problemnew/show/P4711) [算法分析:] 关于一个分子拆分后的产物,一共有三种情况: 原子 原子团 水合物 ...

  9. 关于RBAC(Role-Base Access Control)的理解(转)

    基于角色的访问控制(Role-Base Access Control) 有两种正在实践中使用的RBAC访问控制方式:隐式(模糊)的方式和显示(明确)的方式. 今天依旧有大量的软件应用是使用隐式的访问控 ...

  10. Java中关于CyclicBarrier的使用

    CyclicBarrier工具类主要是控制多个线程的一起执行,CyclicBarrier 实例可以多次使用. 演示程序: import java.util.Random; import java.ut ...