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.该矩阵具有以下特性 ...
随机推荐
- 史上最易懂的大数据 OTO
史上最易懂的大数据 OTO http://network.51cto.com/art/201503/467068.htm 终于有人把O2O.C2C.B2B.B2C的区别讲透了 http://tech. ...
- 用Python xlwt建立excel表格
1.下载xlwt的Python库 (This is a library for developers to use to generate spreadsheet files compatible w ...
- Linux挂载:VMware tools for Linux安装
安装VMTools [root@localhost ~]# mkdir /mnt/cdrom [root@localhost ~]# mount /dev/cdrom /mnt/cdrom/ [roo ...
- C# 二进制文件操作(内容搜索、数据截取)
private void button2_Click(object sender, EventArgs e) { var list = new List<Frame>(); byte[] ...
- css中的clear:both,display:flex;
介绍两者一起讨论的原因: 在明天就国庆的日子里陪着程序员的只有代码,啤酒,还有音乐,然后就是灯光下默默陪伴自己的影子.好了,不矫情了. -------------------------------- ...
- python的socket的学习
一.Socket相关知识 1.socket是什么: socket是应用层与TCP/IP协议族通信的中间软件抽象层,他是一组接口.在设计模式中,Socket其实就是一个门面模式. 它把复杂的TCP/IP ...
- 【Oracle】OGG数据初始化之RMAN
实验环境: 源端.目标端: DataBase:10.2.0.1.0 OS:OEL5.6 OGG:fbo_ggs_Linux_x86_ora11g_32bit 源端使用rman进行备份全库: RMAN& ...
- sql server内存使用情况
查看Sql Server 数据库的内存使用情况 转自:https://www.cnblogs.com/wanghao4023030/p/8299478.html DBCC MemoryStatu ...
- 安卓项目eclipse有用教程:设置应用名字和图标、屏幕、签名、真机调试、clean、logcat、json解析
怎样在安卓项目中.设置游戏的应用名字和图标? 我们在Androidproject的res资源目录下.会看到3个drawable的目录和一个values目录.就是在这里改动即可. 关于改动应用程序名 ...
- codeigniter 中使用 phpexcel
参考:Easily integrate/load PHPExcel into CodeIgniter Framework In order to get PHPExcel working with C ...