/**********************************************************************************************************
7.2 Search Insert Position
描述
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
分析
即 std::lower_bound()
**********************************************************************************************************/

class Solution{
public:int searchInsert(int A[],int n ,int target){return lower_bound(A,A + n,target) - A;}
};

/**********************************************************************************************************
7.3 Search a 2D Matrix
描述
Write an efficient algorithm that searches for a value in an m×n matrix. is matrix has the following
properties:
• Integers in each row are sorted from le to right.
• e first integer of each row is greater than the last integer of the previous row.
For example, Consider the following matrix:7.3 Search a 2D Matrix 117
[
    [1, 3, 5, 7],
    [10, 11, 16, 20],
    [23, 30, 34, 50]
]
Given target = 3, return true.
**********************************************************************************************************/

class Solution{
public:bool searchMatrix(const vecotr<vector<int>>& matrix ,int target){const size_t m = matrix.size();const size_t n = matrix.front().size();int first = 0;int last = m * n;while(first < last){int mid = first + (last - first )/2;int value = matrix[mid /n ][mid %n];if(value == target)return true;else if(value < target)first = mid + 1;elselast = mid;}return false;}
};

参考资料:

LeetCode题解

查找 --- Search Insert Position --- 图解相关推荐

  1. LeetCode - 35. Search Insert Position

    35. Search Insert Position Problem's Link ---------------------------------------------------------- ...

  2. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  3. leetcode -- Search Insert Position

    2019独角兽企业重金招聘Python工程师标准>>> Search Insert Position Given a sorted array and a target value, ...

  4. 【二分法】LeetCode 35. Search Insert Position

    LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...

  5. 【LeetCode】- Search Insert Position(查找插入的位置)

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

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

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

  7. leetcode 【 Search Insert Position 】python 实现

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

  8. 二分法:search insert position 插入位置

    问题描述: 给定一个排序数组nums(无重复元素)与目标值target,如果target在nums里 出现,则返回target所在下标,如果target在nums里未出现,则返回target应该 插入 ...

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

  10. 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. HOOK技术-满足我们程序的偷窥欲
  2. C# 结构体 简明介绍
  3. HDR-100-24N LRS-100-24 漏电测量
  4. C语言写一个简单的数学程序,用C语言计算简单的数学式子
  5. OC 实例变量(instance var)与属性(@property)的关系 isa指针
  6. 解决布局拖动混乱的问题
  7. 15 道超经典大厂 Java 面试题!重中之重
  8. 解决vue+axios post参数后端无法接收的问题
  9. 请求转发和重定向的区别_WEB之重定向和请求转发的区别
  10. str split函数 php,怎么在php中利用str_split函数分割字符串
  11. 动物识别论文整理——一种基于生物特征的鱼类分类模型
  12. 关于x86、x86_64、x64、amd64和arm64、aarch64
  13. 中国新能源汽车电机及控制器行业需求态势与十四五方向分析报告2021年版
  14. 怎样写一封得体的电子邮件
  15. 7、公共电话交换网络(物理层)
  16. huggingFace 中文模型实战——中文文本分类
  17. Lesson 10
  18. [安卓]实现苹果实现的效果之 流光溢彩背景
  19. 【VAR | 时间序列】应用VAR模型时的15个注意点
  20. 大数据 清华 覃征_覃征教授莅临福建省海洋与渔业作题为《大数据对未来社会的影响》的专题报告...

热门文章

  1. idea安装2019
  2. Labview学习笔记(一)
  3. iOS开发_统计xcode代码行数
  4. 蓝桥杯- 煤球数目-java
  5. 利用 TypeConverter,转换字符串和各种类型只需写一个函数
  6. CSS3之3D效果中的transform运用
  7. Java学习笔记四——运算符
  8. UI控件之UITextField
  9. windows7 64位 php5.3安装php_mongo.dll方法 各个版本
  10. 论EFMS模拟量部分采集电路的修改