LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
2024-09-20 10:27:01
240. 搜索二维矩阵 II
240. Search a 2D Matrix II
题目描述
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:
- 每行的元素从左到右升序排列。
- 每列的元素从上到下升序排列。
每日一算法2019/6/9Day 37LeetCode240. Search a 2D Matrix II
示例:
现有矩阵 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]
]
[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。
Java 实现
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int row = matrix.length, col = matrix[0].length;
int i = 0, j = col - 1;
while (i < row && j >= 0) {
if (matrix[i][j] == target) {
return true;
} else if (matrix[i][j] > target) {
j--;
} else {
i++;
}
}
return false;
}
}
相似题目
参考资料
- https://leetcode.com/problems/search-a-2d-matrix-ii/
- https://leetcode-cn.com/problems/search-a-2d-matrix-ii/
最新文章
- 反汇编工具capstone安装后import error
- JSBinding+SharpKit / 菜单介绍
- Nginx 下配置SSL证书的方法
- PLSQL数据导入导出问题解决(空表、大字段表、表空间错误等)
- load css use javascript
- 对于程序开发者看书(指实在的书而不是PDF)的好处。(个人看法而已)
- Tools - Windows
- php--.prop()
- Bash脚本15分钟进阶教程
- iOS事件机制(一)
- 前台之boostrap
- Linux/CentOS下的CST和UTC时间的区别以及不一致的解决方法
- [补档]暑假集训D7总结
- Sparse Principal Component Analysis via Rotation and Truncation
- nghttp2 和nginx的实践
- nginx配置学习总结
- AndrodStudio报错: Cannot launch AVD in emulator.
- Object类(API文档)
- Git 忽略某个目录中的文件,同时保留这个目录
- vlc player验证交换机igmp
热门文章
- *arg和**kwarg作用
- 英语听力,如何成为更好的交谈着https://www.bilibili.com/video/av4279405?from=search&;seid=5889429711390689339
- [NOI2019]回家路线
- Cocos Creator开发hello World
- 【转】Resource Localization in YARN
- python 播放MP3和MP4
- 正则表达式在线分析 regex online analyzer
- 啃OBS源码-界面汉字
- PokemonGo-LBS AR项目实战
- flask 实现最简单的登录功能