Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述
写一个高效的算法。从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点:
每一行从左到右递增。
每一列从上到下递增。
2. 方法与思路
2.1 二分查找法
依据矩阵的特征非常easy想到二分法,可是这是一个二维的矩阵,怎样将问题转化为一维是关键。实际上我们能够依据矩阵的第一列确定值可能所在的行的范围(limu,limd),当中limu=0,使得matrix[0][0]≤matrix[i][0]≤matrix[limd][0],i∈[0,limd]。
而确定limd的值能够使用二分法。
确定了值可能在的行的范围后。逐行在进行二分查找目标值。这样就将问题降到一维上来了。代码例如以下:
class Solution {
public:
bool searchMatrix(vector<vector<int> >& matrix, int target) {
if(matrix.size() == 0) return false;
int i,j,mid,rows = matrix.size(),cols = matrix[0].size();
int limd = rows-1,limu = 0;
/*二分查找目标值可能所在行的下限*/
while(limu < limd)
{
mid = (limu + limd)/2;
if(matrix[mid][0] > target) limd = mid - 1;
else if(matrix[mid][0] < target) limu = mid +1;
else return true;
}
/*对每一行进行二分查找*/
for(i = 0; i <= limd; i++)
{
int l = 0, r = cols-1;
while(l <= r)
{
mid = (l + r)/2;
if(matrix[i][mid] < target) l = mid+1;
else if(matrix[i][mid] > target) r = mid - 1;
else return true;
}
}
return false;
}
};
2.2 分治法
另一种方法就是採用分值的思想。以题目给出矩阵为例,查找数字5。细致观察矩阵,最右上角的数字为15,因为矩阵是列递增,所以数字5不可能在最右側15这一列,我们便可将这一列不予考虑,将范围缩减了一列。
[1, 4, 7, 11]
[2, 5, 8, 12]
[3, 6, 9, 16]
[10, 13, 14, 17]
[18, 21, 23, 26]
再推断数字11。相同11>5,又缩减一列。数字7相同小于5,在缩减一列。那么如今的矩阵变为:
[1, 4,]
[2, 5]
[3, 6]
[10, 13]
[18, 21]
推断数字4时,因为5>4,目标值肯定不在4所在的行,去点这一行,在进行推断。
[2, 5]
[3, 6]
[10, 13]
[18, 21]
Okay,推断数字5,找到目标值返回。
这样的算法时间复杂度O(n),要优于第一种算法。代买例如以下:
class Solution {
public:
bool searchMatrix(vector<vector<int> >& matrix, int target) {
if(matrix.size() == 0) return false;
int i,j,rows = matrix.size(),cols = matrix[0].size();
i = 0;
j = cols-1;
while(i < rows && j >= 0)
{
if(matrix[i][j] == target) return true;
else if(matrix[i][j] > target) j--;
else i++;
}
return false;
}
};
Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)的更多相关文章
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- (medium)LeetCode 240.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 240. 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 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】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】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
随机推荐
- MongoDB资料大全
摘要: 为了帮助大家进一步了解MongoDB,云栖社区组织翻译了GitHub Awesome MongoDB 资源,涵盖MongoDB中常见的库与工具.应用列表.以及相关的文档.教程等资源. Mong ...
- mysqldump参数详细说明
Mysqldump参数大全(参数来源于mysql5.5.19源码) 参数 参数说明 --all-databases , -A 导出全部数据库. mysqldump -uroot -p --al ...
- Chapter 6 -- Caches
CachesExplained Explanation for how to use Guava caches. explained Updated Jun 4, 2013 by lowas...@g ...
- Chapter 4 -- Throwables
TODO: rewrite with more examples Guava's Throwables utility can frequently simplify dealing with exc ...
- [转]分析MySQL数据类型的长度【mysql数据字段 中length和decimals的作用!熟悉mysql必看】
转载自:http://blog.csdn.net/daydreamingboy/article/details/6310907 分析MySQL数据类型的长度 MySQL有几种数据类型可以限制类型的&q ...
- iOS:viewController 和 view 的生命周期、不错的代码设计风格
一.介绍: viwe和viewController的生命周期是最基本的知识,如果很好地理解它们的方法调用的执行顺序,就能很好地设计代码的风格.这篇博客转载自:http://www.cnblogs.co ...
- quantum theory
the principles of quantum mechanics by p.a.m.dirac.
- C++ 内置宏定义 与 预编译指令
内置宏和预编译指令, 在代码调试.单元测试.跨平台代码中经常会用到.这里记录一下. 1. 内置宏 (文件名,当前行号,当前日期,当前时间,当前执行方法名) __FILE____LINE____DATE ...
- android linux 内核层
Android依赖于Linux2.6内核提高的高核心系统服务,例如安全,内存管理,进程管理,网络斎等等方面内容.内核作为一个抽象层,存在与硬件层和软件层之间.android对Linux下面内容做了增强 ...
- 用条件随机场CRF进行字标注中文分词(Python实现)
http://www.tuicool.com/articles/zq2yyi http://blog.csdn.net/u010189459/article/details/38546115 主题 ...