[LC] 74. Search a 2D Matrix
2024-09-07 07:33:30
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 from left to right.
- The first integer of each row is greater than the last integer of the previous row.
Example 1:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true
Example 2:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false
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 start = 0, end = row * col - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
int curRow = mid / col;
int curCol = mid % col;
if (matrix[curRow][curCol] == target) {
return true;
} else if (matrix[curRow][curCol] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return false;
}
}
最新文章
- Java 实现HTML富文本导出至word完美解决方案
- linux 账号管理与ACL权限设定
- 8.1 消息通信 EventBus
- SQL Server 之AdventureWorks 2008 安
- WebGIS空间数据请求访问机制
- 【BZOJ-1588】营业额统计 Splay
- Java多线程基础(一)
- java 创建线程
- java 并发编程
- 快递查询API接口对接方法
- apache设置映射文件夹的配置方法
- A Tour of Go Arrays
- php错误捕捉
- Linux - 引用
- 2 hive的使用 + hive的常用语法
- Struts2(三)——数据在框架中的数据流转问题
- Problem 2128 最长子串(kmp+strstr好题经典)
- mysql各版本区别
- Android 程式开发:(廿二)服务 —— 22.1 自定义服务
- Oracle JDBC版本区别(转)
热门文章
- ubuntu下安装ant
- Docker部署zookeeper集群和kafka集群,实现互联
- 3.react 基础 - JSX 语法
- mysql,apache,php的关系
- python3 str.encode bytes.decode
- java和数据库中所有的锁都在这了
- python学习Day08--文件操作
- urlopen error [errno 10060]的解决思路
- [原]调试实战——使用windbg调试崩溃在ComFriendlyWaitMtaThreadProc
- java数据库执行迁移报错Error creating bean with name &#39;flywayInitializer&#39; defined in class path resource