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]

]

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

相似题目

参考资料

最新文章

  1. 反汇编工具capstone安装后import error
  2. JSBinding+SharpKit / 菜单介绍
  3. Nginx 下配置SSL证书的方法
  4. PLSQL数据导入导出问题解决(空表、大字段表、表空间错误等)
  5. load css use javascript
  6. 对于程序开发者看书(指实在的书而不是PDF)的好处。(个人看法而已)
  7. Tools - Windows
  8. php--.prop()
  9. Bash脚本15分钟进阶教程
  10. iOS事件机制(一)
  11. 前台之boostrap
  12. Linux/CentOS下的CST和UTC时间的区别以及不一致的解决方法
  13. [补档]暑假集训D7总结
  14. Sparse Principal Component Analysis via Rotation and Truncation
  15. nghttp2 和nginx的实践
  16. nginx配置学习总结
  17. AndrodStudio报错: Cannot launch AVD in emulator.
  18. Object类(API文档)
  19. Git 忽略某个目录中的文件,同时保留这个目录
  20. vlc player验证交换机igmp

热门文章

  1. *arg和**kwarg作用
  2. 英语听力,如何成为更好的交谈着https://www.bilibili.com/video/av4279405?from=search&amp;seid=5889429711390689339
  3. [NOI2019]回家路线
  4. Cocos Creator开发hello World
  5. 【转】Resource Localization in YARN
  6. python 播放MP3和MP4
  7. 正则表达式在线分析 regex online analyzer
  8. 啃OBS源码-界面汉字
  9. PokemonGo-LBS AR项目实战
  10. flask 实现最简单的登录功能