题目描述

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

题目大意

给定一个二维整数数组,数组中的行和列都是按照递增顺序排列,要求查找二维数组中是否存在一个给定的数字。

示例

E1

Consider the following matrix:

[[1,   4,  7, 11, 15],[2,   5,  8, 12, 19],[3,   6,  9, 16, 22],[10, 13, 14, 17, 24],[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

解题思路

对每行进行二分搜索,若在当前没有找到给定的数字,则将搜索范围的终止位置缩小为当前位置(因为当在当前行,搜索停止且未找到数字时,说明找到的位置一定大于或小于目标数字,若小于目标数字,则将位置加一,将二分搜索的终止位置重新赋值更新,因为每一行也是递增排列,因此在该位置之后的每一行的数字一定大于目标数字。经过该操作可以减少查询次数)。

复杂度分析

时间复杂度:O(log(N))

空间复杂度:O(1)

代码

class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {if(matrix.size() == 0 || matrix[0].size() == 0)return false;bottom = 0, top = matrix[0].size() - 1;for(int i = 0; i < matrix.size(); ++i) {// 对每一行进行二分查找bool f = binSearch(matrix[i], bottom, top, target);if(f)return true;}return false;}bool binSearch(vector<int>& arr, int sta, int end, int& target) {// 如果二分查找到最后,判断是否找到目标数字,并且将二分范围缩小if(sta >= end) {top = (end == arr.size() - 1 ? top : (arr[end] > target ? end : end + 1));return arr[end] == target;}else {int mid = (sta + end) / 2;bool f;// 若找到直接返回trueif(arr[mid] == target) {return true;}// 否则,若中间位置小于目标数字,则返回后半部分位置else if(arr[mid] < target) {f = binSearch(arr, mid + 1, end, target);return f;}// 否则,若中间位置大于目标数字,则返回前半部分位置else {f = binSearch(arr, sta, mid, target);return f;}}}private:int bottom, top;
};    

转载于:https://www.cnblogs.com/heyn1/p/11114940.html

LeetCode-240 Search a 2D Matrix II相关推荐

  1. LeetCode 240. Search a 2D Matrix II

    LeetCode 240. Search a 2D Matrix II Solution1: 为什么把第74题的代码改都不用改的拿过来就可以AC,一脸懵逼啊... class Solution { p ...

  2. leetcode 240. Search a 2D Matrix II | 240. 搜索二维矩阵 II(Java)

    题目 https://leetcode.com/problems/search-a-2d-matrix-ii/ 题解 方法1 思路类似于 leetcode 200. Number of Islands ...

  3. [Leetcode] 240. Search a 2D Matrix II 解题报告

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  4. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描述 写一个高效的算法,从一个 m×n m\times n的整数矩阵中查找出给定的值,矩阵具有如下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 根据 ...

  5. LeetCode 74. Search a 2D Matrix

    LeetCode 74. Search a 2D Matrix Solution1:我的答案 <剑指offer>原题 class Solution { public:bool search ...

  6. LeetCode Search a 2D Matrix II

    问题:给出一个二维数组,其行,列是递增的,和一个要查找的数,问矩阵中是否有要查找的数 思路: 第一种方法是直接暴力查找,在二维数组中查找. 第二种方法是基于对角线上,在行,列上作二分查找 第三种方法递 ...

  7. [Leetcode] 74. Search a 2D Matrix 解题报告

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  8. 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...

  9. LeetCode 74. Search a 2D Matrix--有序矩阵查找--python,java,c++解法

    题目地址:Search a 2D Matrix - LeetCode Write an efficient algorithm that searches for a value in an m x ...

最新文章

  1. hibernate保存会先insert再update_Hibernate的批处理
  2. mysql中lock_mysql中的锁lock
  3. Feature Tools:可自动构造机器学习特征的Python库
  4. (十五)nodejs循序渐进-高性能游戏服务器框架pomelo之Protobuf模块
  5. Xadmin添加用户小组件出错
  6. PaperWeekly 第十一期
  7. 在MacBook上Jupyter安装
  8. 20201221:力扣220场周赛题解
  9. 2.1.3 JavaScript代码书写规则
  10. 异常问题解决Error:Execution failed for task ‘:app:processDebugManifest‘
  11. java.sql找不到_java.sql.SQLException:找不到适用于jdbc:microsoft:
  12. Python selenium报错:selenium.common.exceptions.ElementClickInterceptedException
  13. yolov4-tiny目标检测模型实战——学生姿势行为检测
  14. avr 运行 linux,linux(ubuntu9.04)安装avr编译环境
  15. 【爬虫实战】 58同城爬虫-商铺信息采集
  16. html5中required标签必填项
  17. 记一次Comparator.comparing(XXX::getStartTime).reversed()失效
  18. 温度传感器的分类及比较
  19. 移动终端管理系统的关键技术研究 - 转贴
  20. 基于vmware16 和 ubuntu20.04, 搭建单节点 kubernetes 1.22.2

热门文章

  1. 作业帮电脑版在线使用_互助作业帮PC版-互助作业帮电脑版下载 v4.5.8
  2. (6) ebj学习:ejb使用jpa注解
  3. (三) shiro通过jdbc连接数据库
  4. 王爽汇编习题 : 给定段地址为0001H,仅通过变化偏移地址寻址,CPU的寻址范围为 ___ 到 ____。
  5. C实现二叉树的先序遍历,中序遍历,后序遍历
  6. android监听方法的耗时时间,Android Studio监听时间变化
  7. java object 详解_Java基础之Object类详解
  8. jsonschema中的$ref的循环引用导致json-editor栈溢出
  9. 模型评估准确率、召回率、ROC曲线、AUC总结
  10. geometry-api-java 学习笔记(一)概述