当前位置:网站首页>剑指Offer 04.二位数组中的查找 线性查找
剑指Offer 04.二位数组中的查找 线性查找
2022-08-02 03:33:00 【HotRabbit.】
题目
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
示例:
现有矩阵 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]
]
给定 target = 5
,返回 true
。
给定 target = 20
,返回 false
。
限制:
0 <= n <= 1000
0 <= m <= 1000
**注意:**本题与主站 240 题相同:https://leetcode-cn.com/problems/search-a-2d-matrix-ii/
- Related Topics
- 数组
- 二分查找
- 分治
- 矩阵
思路
之前在刷二分的题时,刷到过这道题,所以一开始就想着用二分做,结果发现其实就是暴力+二分的解法
突然想到从最左下角的元素开始判断,然后就写出来了~
题解
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix.length == 0){
return false;
}
int n = matrix.length - 1;
int m = 0;
while (n >= 0 && m <= matrix[0].length - 1){
if (matrix[n][m] == target){
return true;
}else if (matrix[n][m] < target){
m++;
}else {
n--;
}
}
return false;
}
}
边栏推荐
- DMA相应外设映射
- Type c PD 电路设计
- MQ-5 combustible gas sensor interface with Arduino
- 改变文件的扩展名
- 振芯科技GM8285C:功能TTL转LVDS芯片简介
- Comparative analysis of mobile cloud IoT pre-research and Alibaba Cloud development
- Process (in): process state, process address space
- MIPI解决方案 ICN6202:MIPI DSI转LVDS转换芯片
- 【nRF24L01 connects with Arduino to realize wireless communication】
- Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
猜你喜欢
云服务器web项目部署详解
Comparative analysis of mobile cloud IoT pre-research and Alibaba Cloud development
AD8307对数检波器
【TCS3200 color sensor and Arduino realize color recognition】
剑指Offer 34.二叉树中和为某一值的路径 dfs+回溯
【Popular Science Post】UART Interface Communication Protocol
改变文件的扩展名
Comparison between Boda Industrial Cloud and Alibaba Cloud
【LeetCode】求和
Lightly:新一代的C语言IDE
随机推荐
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
STM32 CAN 介绍以及相关配置
DMA相应外设映射
Host your own website with Vercel
LT8918L LVDS转MIPI芯片技术支持资料
【详解】线程池及其自定义线程池的实现
谷粒商城10——搜索、商品详情、异步编排
MAC安装Mysql超详细完整教程
MQ-5 combustible gas sensor interface with Arduino
Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
引擎开发日志:集成Bullet3物理引擎
TQP3M9009电路设计
vector的使用和模拟实现:
使用飞凌嵌入式IMX6UL-C1板子——qt+opencv环境搭建
引擎开发日志:重构骨骼动画系统
剑指Offer 36.二叉搜索树与双向链表 中序遍历
408-Binary tree-preorder inorder postorder level traversal
IDEA2021.2安装与配置(持续更新)
调试九法准则
Industry where edge gateway strong?