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 ...
随机推荐
- JSON字符串与java对象的转换
所需的jar包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar ...
- [转]ASP.NET会话(Session)保存模式
本文转自:http://blog.csdn.net/cityhunter172/article/details/727743 作者:寒羽枫(cityhunter172) 大家好,已有四个多月没写东东啦 ...
- 将组策略中的内容导出至CSV格式
#将组策略首选项中的"本地用户和组"下的所有条目导出 $xml = Get-GPOReport -ReportType Xml -Name "China Desktop ...
- css样式重置方案 -解决浏览器差异
1.http://meyerweb.com/eric/tools/css/reset/ /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 2011 ...
- $.each(),$.map()归纳
//$.each()对字典(没有索引).数组(有索引) 遍历 //两个参数 var json={"name":"李可","age":&quo ...
- The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...
- mysql insert一条记录后怎样返回创建记录的主键id,last_insert_id(),selectkey
mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...
- 如何查询MySql日志
如何查询MySql日志 分类: mysql2012-02-23 19:14 26756人阅读 评论(2) 收藏 举报 mysqlcommandprintingserversocketoutput 今天 ...
- PHP程序员必须清楚的问题汇总
PHP程序员必须清楚的问题汇总 投稿:hebedich 字体:[增加 减小] 类型:转载 这篇文章主要介绍了PHP程序员必须清楚的问题汇总,需要的朋友可以参考下 你是否正在准备寻找一份PH ...
- STM32全球唯一ID读取方法
产品唯一的身份标识非常适合:● 用来作为序列号(例如USB字符序列号或者其他的终端应用)● 用来作为密码,在编写闪存时,将此唯一标识与软件加解密算法结合使用,提高代码在闪存存储器内的安全性.● 用来激 ...