Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。
问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找。
算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法。
public class SearchInSortedMatrix
{
public static boolean searchMatrix(int[][] matrix, int target)
{
int m = matrix.length;
int n = matrix[0].length;
int low = 0;
int high = m*n - 1;
while(low <= high)
{
int mid = (low + high)/2;
int row = mid / n;
int column = mid % n;
if(matrix[row][column] == target)
{
return true;
}
else if(matrix[row][column] < target)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return false;
}
}
问题描述:二维矩阵行有序,列有序,进行查找。
算法分析:有两种方法,一种是将矩阵按中心点分成左上,左下,右上,右下,四部分,进行递归查找。
还有一种比较巧妙的查找方法,就是从左下角或者右上角的元素进行查找。
//矩阵每一行有序,每一列有序
public boolean searchMatrix2(int[][] matrix, int target)
{
int m = matrix.length;
int n = matrix[0].length;
return helper(matrix, 0, m-1, 0, n-1, target);
}
public boolean helper(int[][] matrix, int rowStart, int rowEnd, int colStart, int colEnd, int target)
{
int rm = (rowStart + rowEnd)/2;
int cm = (colStart + colEnd)/2;
if(rowStart > rowEnd || colStart > colEnd)
{
return false;
}
if(matrix[rm][cm] == target)
{
return true;
}
else if(matrix[rm][cm] > target)
{
return helper(matrix, rowStart, rm - 1, colStart, cm - 1, target)
|| helper(matrix, rm, rowEnd, colStart, cm - 1, target)
|| helper(matrix, rowStart, rm - 1, cm, colEnd, target);
}
else
{
return helper(matrix, rm + 1, rowEnd, cm + 1, colEnd, target)
|| helper(matrix, rm + 1, rowEnd, colStart, cm, target)
|| helper(matrix, rowStart, rm, cm + 1, colEnd, target); }
} //从右上角元素进行查找
public boolean searchMatrix3(int[][] matrix, int target)
{
int m = matrix.length;
int n = matrix[0].length;
int low = 0;
int high = m*n - 1;
while(low <= high)
{
int mid = (low + high)/2;
int row = mid / n;
int column = mid % n;
if(matrix[row][column] == target)
{
return true;
}
else if(matrix[row][column] < target)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return false;
}
Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。的更多相关文章
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...
- [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 搜索一个二维矩阵
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 ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [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 ...
- 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.该矩阵具有以下特性 ...
随机推荐
- Taylor's theorem
w https://en.wikipedia.org/wiki/Taylor_series
- openssl update--centos 6.5
cd /usr/local/src #wget http://www.openssl.org/source/openssl-1.0.1h.tar.gz #tar -zxvf openssl-1.0.1 ...
- 8.Query Documents-官方文档摘录
总结 1 先插入数据 db.inventory.insertMany([ { item: "journal", qty: 25, size: { h: 14, w: 21, uom ...
- 初识python(二)
初识python(二) 1.变量 变量:把程序运行的中间结果临时的存在内存里,以便后续的代码调用. 1.1 声明变量: #!/usr/bin/env python # -*- coding: utf- ...
- django博客项目7
................
- Python 模块之 time & datetime
Python 中提供了对时间日期的多种多样的处理方式,主要是在有 time 和 datetime 两个模块. time 在 Python 文档里,time 是归类在 Generic Operating ...
- 利用Docker快速部署Oracle环境
工作中需要频繁使用Oracle环境,但是每次搭建起来比较消耗时间,本想通过虚拟机模板的方式来快速安装oracle vm,但是每次改ip等环境也很耗时,因此想到docker中有没有已经做好的images ...
- java中jdk安装配置信息
由于被人比较懒,有些做过的事情总是好忘,这个也不例外,索性就做个随笔. JAVA_HOMEC:\Program Files (x86)\Java\jdk1.7.0_03CLASSPATH.;%JAVA ...
- 单文件快速体验使用react输出hello_world
看了下react官方的hello world教程, 感觉对新手很不友好.codepen虽然好用, 但是封装太多东西, 看起来 太抽象. 还是喜欢像学习jQuery那样, 直接在单文件中引入必要的js文 ...
- C# 委托及匿名函数
一. 为什么使用委托,代码如下:(注释掉的是没用委托之前的使用方式,没有注释的是使用了委托的) public delegate string DelProStr(string name); class ...