问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找。

算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法。

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,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。的更多相关文章

  1. [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找

    题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  7. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  8. [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 ...

  9. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

  10. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

随机推荐

  1. 机械迷城MAC下载及攻略

    点击下载 无意间在verycd上看到这个游戏,很好玩的一个游戏. 画风非常可爱,有点复古风. 这里是 机械迷城 的专题频道 http://pc.pcgames.com.cn/pczq/jxmc/

  2. APP测试瞎话

    APP测试        一.功能性        1.APP的安装.卸载        2.APP中业务功能            二.性能测试        1.高.中.低端机上运行效果      ...

  3. orchestrator-Raft集群部署

    本文简要说明下orchestrator的Raft集群部署,其实部署很简单主要是好好研究下配置文件的配置,这里我的样例配置文件暂时只适用于我们这块业务 如果您自己使用请根据情况自行修改. 主要通过配置文 ...

  4. Oracle database精装版11gR2入门详细连接教程

    对于11g本身比较简单,适合学习者使用,对电脑要求相对较低. 自己一个人单机学习使用. 工具/原料   Oracle Database Express Edition 11g Release 2安装包 ...

  5. visual studio 下 C++生成dump文件

    1 lib配置 项目-->属性-->配置属性-->链接器-->输入-->附加依赖项 增加dbghelp.lib 2 头文件 #include <imagehlp.h ...

  6. Milking Time---poj3616(简单dp)

    题目链接:http://poj.org/problem?id=3616 题意:人从奶牛身上挤奶有m个时间段(1----n),每个时间段包含 s e f 表示从 s 到 e 的这段时间可以获得 f 单位 ...

  7. 004-React入门概述

    一.概述 参考地址:https://reactjs.org/docs/try-react.html 1.1.本地快速体验 <!DOCTYPE html> <html> < ...

  8. Android学习十---Android Camera

    Android camera用来拍照和拍摄视频的先看一下最后实现的效果图             最后的效果图 一.准备 在你的应用程序上使用android拍照设备,需要考虑以下几个方面 1. 是否是 ...

  9. mysql密码忘记该怎么办?

    环境:linux;mysql5.7 mysql密码忘记: [root@izwz9f40l0qo5cpnn8qwmpz ~]# mysql -u root -pEnter password: ERROR ...

  10. Redis五(其他操作命令)

    其他常用操作 delete(*names) # 根据删除redis中的任意数据类型 exists(name) # 检测redis的name是否存在 keys(pattern='*') # 根据模型获取 ...