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 ...
随机推荐
- TCP/IP协议原理与应用笔记06:OSI参考模型全称
1. ISO是一个组织,OSI是一个模型. 2. OSI不是协议,是网络体系结构的概念模型 3. OSI参考模型全称为: Open System Interconnection Reference M ...
- 新一代 PHP 加速插件 Zend Opcache <转>
注: 由于原链接已不存在, 所以我把图片重新整理了一下, 以便看起来更加直观 笔者注: 1> PHP 性能提升之 PHP NG => php next generation wiki ...
- html.day02
1.链接标签 a <a href=”http://www.baidu.com”></a> <img src=”aaa”/> 一般情况下: 来源 用 src ...
- 【转】深入理解Java:SimpleDateFormat安全的时间格式化
[转]深入理解Java:SimpleDateFormat安全的时间格式化 想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用 ...
- 黑马程序员-ReadInt
判断输入的字符串是否为数字. namespace 读入一个整数 { class Program { static void Main(string[] args) { Console.WriteLin ...
- EXPDP IMPDP 知识总结
Data Pump Export ATTACH Default: job currently in the user's schema, if there is only one Purpose(目的 ...
- oracle死锁模拟
环境介绍: 用户test01 创建表tab01,用户test02创建表tab02.Test01 更新tab01不提交,test02 更新表tab02不提交.然后test01 更新test02下的表ta ...
- UISegmentControl 、UIStepper
UISegmentControl .UIStepper UISegmentControl 1. UISegmentedControl *segmentControl = [[UISegmentedCo ...
- c#读取通达信历史数据的方法
public Bar ReadBarMin(BinaryReader br, int instrumentId, long size) { int date = br.ReadUInt16(); in ...
- java_reflect_01
最近学习java开始接触到了框架,突然觉得java反射很重要,因此在这里做了一些总结(参考园中大苞米大神的文章) 首先我们要认识一下Class: 一.Class类有什么用? class类的实例表示ja ...