LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/
题目:
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 in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following 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]
]
Given target = 5
, return true
.
Given target = 20
, return false
.
题解:
本题是每一行每一列都是升序,从右上角开始x = matrix[i][j], i = 0, j = matrix[0].length - 1, 用x 和target 比较,若是相等则返回true.
若是小于target, 说明肯定不会在这一行,所以i++. 若是大于target, 说明必然不会在这一列所以 j--.
若走到边界还没有返回说明没有这个target, 返回false.
Note: j的初始量是 j = n-1, 注意减一.
Time Complexity: O(m+n). m = matrix.length. n = matrix[0].length.
Space: O(1).
AC Java:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int i = 0;
int j = n-1; //error
while(i<m && j>=0){
int x = matrix[i][j];
if(x == target){
return true;
}else if(x < target){
i++;
}else{
j--;
}
}
return false;
}
}
跟上Kth Smallest Element in a Sorted Matrix.
LeetCode Search a 2D Matrix II的更多相关文章
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
- LeetCode——Search a 2D Matrix II
Description: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- 【刷题-LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
随机推荐
- 使 SortList 实现重复键排序
SortList 默认对按Key来排序,且Key值不能重复,但有时可能需要用有重复值的Key来排序,以下是实现方式: 1.对强类型:以float为例 #region 使SortList能对重复键排序 ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '??????' in 'field list'
严重: Servlet.service() for servlet jsp threw exceptioncom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErro ...
- 8.0 Qweb 报表编写步骤
8.0 采用的是Qweb报表,摒弃了7.0中的RML报表. 1.首先在xml文件中注册一个报表: <report id="qweb_test_report" model=&q ...
- state配置语言实战
修改配置文件:(base用来放初始化环境.prod用来放生产配置环境) [root@super65 ~]# vim /etc/salt/master [root@super65 ~]# mkdir - ...
- PHPExcel 是用来操作Office Excel 文档的一个PHP类库
PHPExcel 是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言.可以使用它来读取.写入不同格式的电子表格,如 Excel (BIFF) .xls ...
- C#并行库(TaskParallelLibrary)用法小结
今天有空,总结一下.NET 4.5并行库(TaskParallelLibrary)用法. 也许C和C++的程序员刚刚开始写C#还习惯于new Thread来新建一个线程,但新建线程需要内存和CPU上下 ...
- [排错] Status error 2850
前几天重新搭建了APAC的Netbackup, 终于可以备份成功了, 但是今天在做还原的时候遇到了些小问题,记录下来. 1. 在CNHZSRV04BPO上执行还原的时候报下面的错误. 2. 经过检查发 ...
- PHP 错误与异常 笔记与总结(4)通过 trigger_error 触发 PHP 错误
[错误抑制符@] 除了 php.ini 中 error_reporting 和 display_errors 的设置.error_reporting() 函数.ini_set() 函数 外,还可以使用 ...
- 获取checkbox复选框的值
<title>CheckBox</title> <script type = "text/javascript"> doGetVal: func ...
- ecshop数据库操作函数
ecshop数据库操作函数 分类: ecshop和dede2013-09-23 14:02 1716人阅读 评论(0) 收藏 举报 本章我们将结合eschop二次开发一些常见的开发例子.来谈谈ecsh ...