Search a 2D Matrix ——LeetCode
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
.
题目大意:给一个m*n矩阵,每一行都是递增有序,逐行也是递增的,要求设计一个高效算法检查目标元素是否存在于此矩阵中。
解题思路:可以把整个矩阵展开,就是一个长数组,数组长度为M*N,用二分查找即可,就是需要把二分查找的位置转化为矩阵下标。假设有row行,col列,那么key对应的矩阵中的元素应该是matrix[key/col][key%col],这样就转为二分查找了。
Talk is cheap>>
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix[0][0] > target)
return false;
int rowLen = matrix.length;
int colLen = matrix[0].length;
int low = 0, high = rowLen * colLen - 1; while (low <= high) {
int mid = (low + high) >>> 1;
int x = mid / colLen;
int y = mid % colLen;
if (matrix[x][y] > target) {
high = mid - 1;
} else if (matrix[x][y] < target) {
low = mid + 1;
} else {
return true;
}
}
return false;
}
Search a 2D Matrix ——LeetCode的更多相关文章
- 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 f ...
- 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 搜索一个二维矩阵之二
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 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(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- mysql中数据库database、实例instance、会话session的关系
1. No suitable driver found for http://127.0.0.1:3306/test jdbc_url错误,jdbc走自己的协议. 正确的路径应该是:jdbc:mysq ...
- 从实践谈iOS生命周期
从实践谈iOS生命周期 个人感觉生命周期无论在Android,还是iOS都是很重要的概念,因为在每个声明周期的状态下我们可以做很多预加载或者处理的操作.因此在这里主要总结下ViewController ...
- Java基础知识强化之IO流笔记05:try...catch...finally包含的代码是运行期的
1. 代码示例: 上面看到的第13行: Date d = null;(这里必须初始化) 第14~20行使用try...catch...finally包含代码,这里的代码已经变成运行期代码.此时我们 ...
- RHEL7下PXE+NFS+Kickstart无人值守安装操作系统
RHEL7下PXE+NFS+Kickstart无人值守安装操作系统 1.配置yum源 vim /etc/yum.repos.d/development.repo [development] name= ...
- ASP.NET过滤器、URL重写
过滤器可以对指定的URL访问进行拦截,并执行过滤器的方法,根据实际应用情况,在过滤器中修改请求的代码.判断会话信息,也可以做权限控制. 一:编写过滤器,实现URL重写并配置过滤 编写过滤器,就是编写一 ...
- 安装php时,make test报错
Build complete. Don't forget to run 'make test'. [root@localhost php-5.4.5]# make test ERROR: Cannot ...
- iOS面试小题集锦
1.Object-C有多继承吗?没有的话用什么代替? cocoa 中所有的类都是NSObject 的子类 多继承在这里是用protocol 委托代理 来实现的你不用去考虑繁琐的多继承 ,虚基类的概 ...
- Java中的继承与组合
本文主要说明Java中继承与组合的概念,以及它们之间的联系与区别.首先文章会给出一小段代码示例,用于展示到底什么是继承.然后演示如何通过“组合”来改进这种继承的设计机制.最后总结这两者的应用场景,即到 ...
- C#语言的新特性及相关信息
.ENT版本 NET 2.0 :CLR, WinForms ,Wed Services,ASP.NET NET 3.0 :WCF,WF,WPF,CardSpace NET 3.5 :LINQ ,AJ ...
- jQuery截取字符串插件区分中英文
jQuery截取字符串插件区分中英文:截取字符串功能在大量网站都有应用,比如新闻列表这样的功能,因为新闻的标题长途未必都是恰如其分的,所以要根据需要截取指定长度的字符串,下面就分享一个jQuery实现 ...