当前位置:网站首页>LeetCode:剑指 Offer 04. 二维数组中的查找
LeetCode:剑指 Offer 04. 二维数组中的查找
2022-07-06 08:44:00 【Bertil】
在一个 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/
解题思路
1.首先定义出左下角的坐标,
2.然后从左下角元素开始遍历矩阵:若当前元素 > target,则上移一行;若当前元素 < target,则右移一列;若当前元素 = target,则直接返回true即可
3.最后通过上述循环遍历查找后,若未找到等于target的元素则直接返回false即可
代码
/** * @param {number[][]} matrix * @param {number} target * @return {boolean} */
var findNumberIn2DArray = function(matrix, target) {
if(!matrix.length) return false;
// 定义左下角的坐标
let [row, col] = [matrix.length - 1, 0];
const len = matrix[0].length;
// 坐标在矩阵内,就一直查找
while (row >= 0 && col <= len - 1) {
// item 表示当前元素
const item = matrix[row][col];
if (item === target) {
// 找到,返回true
return true;
} else if (item > target) {
// 太大了,上移一行
row--;
} else {
// 太小了,右移一列
col++;
}
}
return false;
};
边栏推荐
- On the inverse order problem of 01 knapsack problem in one-dimensional state
- Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
- R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
- 按位逻辑运算符
- Visual implementation and inspection of visdom
- Is it safe to open an account in Zheshang futures?
- PC easy to use essential software (used)
- 深度剖析C语言指针
- sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
- Variable length parameter
猜你喜欢

Sublime text using ctrl+b to run another program without closing other runs
![[embedded] print log using JLINK RTT](/img/22/c37f6e0f3fb76bab48a9a5a3bb3fe5.png)
[embedded] print log using JLINK RTT
![[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes](/img/ac/773ce8ee7f380df19edf8373250608.jpg)
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes

sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题

Indentation of tabs and spaces when writing programs for sublime text
![[MySQL] database stored procedure and storage function clearance tutorial (full version)](/img/27/e775e03b77c7195216bc50c5cbefb4.png)
[MySQL] database stored procedure and storage function clearance tutorial (full version)

JVM performance tuning and practical basic theory - Part 1

Excellent software testers have these abilities

2022.02.13 - NC002. sort

游戏解包的危害及资源加密的重要性
随机推荐
Trying to use is on a network resource that is unavailable
Precise query of tree tree
How to conduct interface test? What are the precautions? Nanny level interpretation
TP-LINK enterprise router PPTP configuration
电脑F1-F12用途
What is CSRF (Cross Site Request Forgery)?
[embedded] cortex m4f DSP Library
UnsupportedOperationException异常
Current situation and trend of character animation
[brush questions] top101 must be brushed in the interview of niuke.com
Verrouillage [MySQL]
Image, CV2 read the conversion and size resize change of numpy array of pictures
TP-LINK 企业路由器 PPTP 配置
延迟初始化和密封类
ESP8266-RTOS物联网开发
Promise 在uniapp的简单使用
ROS编译 调用第三方动态库(xxx.so)
Function coritization
torch建立的网络模型使用torchviz显示
Beijing invitation media