Search a 2D Matrix leetcode java
题目:
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.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
题解:
虽然本题看似是矩阵问题,但是本着搜索题目关键字为第一步的原则,可以找到:each row are sorted,每一行按照顺序也是sorted。同时也是数组保存。
但是本题的难点就是如何将2D矩阵转换成1D,然后利用二分查找法来解决问题。转换的重点就在于每个点的位置,在矩阵表示中,我们习惯用(i,j)来表示一个点,所以这就有碍于我们使用low high mid来指向需要的位置。为了解决问题,第一步就是需要将这个矩阵按照顺序拉成一条线。
像题中的例子我可以将其转化为:
position: 0 1 2 3 4 5 6 7 8 9 10 11
values: 1 3 5 7 10 11 16 20 23 30 34 50
row: 0 0 0 0 1 1 1 1 2 2 2 2
column: 3 0 0 2 3
其中:行数rows=3,列数columns=4
如上,这个就是将2D矩阵转化成1行数组的对应表。所以对于二分查找法的初始值为:low=0,high=rows*columns-1(总共数值的个数,因为从0开始所以减1)。而为了能够方便在given 2D matrix找到需要比对的值,我们还是需要确定行数和列数,通过上表可以看出,行数是position/columns,而列数是position%columns, 这样一来,就能很容易的在原矩阵中定位到所需要的值。剩下其他的解题思路,就与二分查找法一模一样了。
时间复杂度O(log(rows*columns))
代码如下:
1 public boolean searchMatrix(int[][] matrix, int target) {
2 if(matrix.length==0||matrix[0].length==0||matrix==null)
3 return false;
4
5 int rows = matrix.length;
6 int cols = matrix[0].length;
7
8 int low = 0;
9 int high = rows*cols-1;
while(low<=high){
int mid = (low+high)/2;
int midValue = matrix[mid/cols][mid%cols];
if(midValue == target)
return true;
else if(midValue < target)
low = mid+1;
else
high = mid-1;
}
return false;
}
同时,也有另外一个解决该题的方法,就是利用两次二分查找法。因为所给矩阵第一列也是升序排列的,所以可以先对第一列进行二分查找,锁定该元素所在行数,然后再对列进行二分查找,即可判断target是否存在。这个的算法时间复杂度是O(log(rows)+log(columns))。
代码如下:
1 public boolean searchMatrix(int[][] matrix, int target) {
2 if(matrix == null || matrix.length==0 || matrix[0].length==0)
3 return false;
4 int low = 0;
5 int high = matrix.length-1;
6 while(low<=high){
7 int mid = (low+high)/2;
8 if(matrix[mid][0] == target)
9 return true;
else if(matrix[mid][0] > target)
high = mid-1;
else
low = mid+1;
}
int row = high; //当从while中跳出时,low指向的值肯定比target大,而high指向的值肯定比target小
if(row<0)
return false;
low = 0;
high = matrix[0].length-1;
while(low<=high){
int mid = (low+high)/2;
if(matrix[row][mid] == target)
return true;
else if(matrix[row][mid] > target)
high = mid-1;
else
low = mid+1;
}
return false;
}
Reference:
http://www.programcreek.com/2013/01/leetcode-search-a-2d-matrix-java/
http://blog.csdn.net/linhuanmars/article/details/24216235
Search a 2D Matrix leetcode java的更多相关文章
- Search a 2D Matrix ——LeetCode
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Search a 2D Matrix leetcode
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
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- [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 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- LeetCode: Search a 2D Matrix 解题报告
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
随机推荐
- 使用DNSPod域名解析
1 在GoDaddy域名注册商 注册域名 https://sg.godaddy.com/zh/ 2 登陆DNSPod https://www.dnspod.cn 3 选择域名解析 添加域名 4 添加记 ...
- 基于特征码文件恢复工具magicrescue
基于特征码文件恢复工具magicrescue 常见类型的文件都包含一些特殊的字节,用来标识文件的类型.这些字节被称为特征码.在磁盘中,当记录文件存储位置的簇损坏后,就可以基于这些特征码来恢复文件. ...
- DHTML参考手册
中文DHTML参考手册 本dhtml教程由宏博内容管理系统为它的Smarty模板制作者收集的,目的是可以做出更加精美的模板.下面列出了由动态 HTML 定义的对象.DHTML中文参考手册,实用dhtm ...
- 在Kali Linux上编译Windows EXP
使用vc6.0去编译的时候,难免会出现点问题 这里找到MS11-046的exp来编译 poc地址:https://www.exploit-db.com/exploits/40564/ 首先需要安装mi ...
- 第一章--Go与web应用
Go语言构建web应用的特性 可扩展 可维护 模块化 高性能 HTTP简介 HTTP是一种无状态.由文本构成的请求-响应(request-response)协议,这种协议使用的是客户端-服务器(cli ...
- bzoj 3653
每个点维护一颗以深度为下标,size-1为值的线段树,保存整颗子树的信息,这样就可以查询了,但是如果为每个节点都建立这么一颗树,显然会MLE,所以考虑在DFS序上建立主席树,然后每个节点原来对应的线段 ...
- python开发_HTMLParser_html文档解析
''' 在HTMLParser类中,定义了很多的方法,但是很多方法都是没有实现的, 这需要我们继承HTMLParser类,自己去实现一些方法 如: # Overridable -- handle st ...
- Apache 如何反向代理tomcat并且实现Session保持
简介 LAMT=Linux+Apache+MySQL+Tomcat: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器: 在中小型系统和并发访问用户不是很多的场合下 ...
- SMB协议概述
一.概述 SMB(Server Message Block)是由微软开发的一种软件程序级的网络传输协议,主要用来使得一个网络上的计算机共享计文件.打印机.串行端口和通讯等资源.它也提供认证的进行进程间 ...
- kNN(K-Nearest Neighbor)最邻近规则分类
KNN最邻近规则,主要应用领域是对未知事物的识别,即推断未知事物属于哪一类,推断思想是,基于欧几里得定理,推断未知事物的特征和哪一类已知事物的的特征最接近: K近期邻(k-Nearest Neighb ...