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.该矩阵具有以下特性 ...
随机推荐
- XMLHttpRequest 对象 status 和statusText 属性对照表
XMLHttpRequest 对象 status 和statusText 属性对照表 status statusText 说明 0** - 未被始化 1** - 请求收到,继续处理 100 Conti ...
- LeetCode-Pathcing Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- Openstack块存储cinder安装配置
openstack service create --name cinderv2 \ --description "OpenStack Block Storage" volumev ...
- Oracle http://127.0.0.1:8080/apex无法访问解决方案
造成无法访问的原因多数情况是由于Oracle中TNS的配置发生了改变. 造成TNS配置有问题的原因可能是:1. 修改了计算机名 2. 修改了IP 默认oracleXE 启动OracleXETNS ...
- Group By 与 Count
select UserID,COUNT(0) From [Order] a Group By UserID UserID (无列名)1 5 2 ...
- Java基础语法 - 面向对象 - 类的主方法main方法
主方法是类的入口点,它指定了程序从何处开始,提供对程序流向的控制.Java编译器通过主方法来执行程序. 主方法的语法如下: /* a.主方法是静态的,如果要直接在主方法中调用其它方法,则该方法必须也是 ...
- 初识Locust---认识
性 能测试工具: 基于Python的性能测试工具-locust 现在性能测试方面有很多测试工具,比如我们熟悉的loadrunner.jmeter.ab等,用过的也就是这几种,如果是学过这些工具的可能对 ...
- windows下的Mysql安装与基本使用(msi)
一.安装方式 1.msi(其他版本:https://www.cnblogs.com/zjiacun/p/6653891.html) 2.zip 这里我们用msi吧,只是单纯练习的话,简单很多 二.ms ...
- selenium破解数字验证码
搞了半天,总算弄出来了,识别率还可以,普通的数字验证码 from selenium import webdriver from PIL import Image import pytesseract ...
- JavaScript中堆栈解析,已经与delete之间的关系。
1,在栈中的数据不会随意删除. 2,堆中的数据可以随意删除. 注意:用eval("var a")定义的变量存放在栈中. var 和function 语句在JavaScript中的优 ...