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;
}
}

最新文章

  1. Java 实现HTML富文本导出至word完美解决方案
  2. linux 账号管理与ACL权限设定
  3. 8.1 消息通信 EventBus
  4. SQL Server 之AdventureWorks 2008 安
  5. WebGIS空间数据请求访问机制
  6. 【BZOJ-1588】营业额统计 Splay
  7. Java多线程基础(一)
  8. java 创建线程
  9. java 并发编程
  10. 快递查询API接口对接方法
  11. apache设置映射文件夹的配置方法
  12. A Tour of Go Arrays
  13. php错误捕捉
  14. Linux - 引用
  15. 2 hive的使用 + hive的常用语法
  16. Struts2(三)——数据在框架中的数据流转问题
  17. Problem 2128 最长子串(kmp+strstr好题经典)
  18. mysql各版本区别
  19. Android 程式开发:(廿二)服务 —— 22.1 自定义服务
  20. Oracle JDBC版本区别(转)

热门文章

  1. ubuntu下安装ant
  2. Docker部署zookeeper集群和kafka集群,实现互联
  3. 3.react 基础 - JSX 语法
  4. mysql,apache,php的关系
  5. python3 str.encode bytes.decode
  6. java和数据库中所有的锁都在这了
  7. python学习Day08--文件操作
  8. urlopen error [errno 10060]的解决思路
  9. [原]调试实战——使用windbg调试崩溃在ComFriendlyWaitMtaThreadProc
  10. java数据库执行迁移报错Error creating bean with name &#39;flywayInitializer&#39; defined in class path resource