Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target =3, returntrue.

题意:在一个二维矩阵中,查询一个数是否存在。数组:1)每行从左到右从下到大排好;2)行首元素大于上一行的最后一个元素;

思路:常规思路:先遍历行找到元素所可能在的行,然后遍历列,判断是否在在该行中,时间复杂度O(n+m);二分查找版本一:是对常规思路的升级,先查找行 ,再查找列,但这时使用的查找的方法不是从头到尾的遍历,是二分查找,值得注意的是查找完行以后的返回值,时间复杂度O{logn+logm)二分查找版本二:因为矩阵数排列的特性,可以看成一个排列好的一维数组[0, n*m],可以针对整个二维矩阵进行二分查找,时间复杂还是O(log(n*m)),这里的难点是,如何将二维数组的下标和一维数组之间进行转换。

方法一:

 class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target)
{
int row = matrix.size();
int col = matrix[].size();
int subRow = ;
if (row == || col == ) return false; //寻找行
if (matrix[row - ][] <= target) //最后一行,特殊处理
subRow = row - ;
else
{
for (int i = ; i<row - ; ++i)
{ if ((matrix[i][] <= target) && (matrix[i + ][]>target))
{
subRow = i;
break;
}
}
} //查找列
for (int j = ; j<col; ++j)
{
if (matrix[subRow][j] == target)
return true;
}
return false;
}
};

方法二:如下:

 // Two binary search
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target)
{
int row=matrix.size();
int col=matrix[].size();
if (row== || col==) return false;
if (target < matrix[][] || target > matrix[row-][col-]) return false; //查找行
int lo = , hi = row - ;
while (lo <= hi)
{
int mid = (lo+hi) / ;
if (matrix[mid][] == target)
return true;
else if (matrix[mid][] < target)
lo = mid + ;
else
hi = mid - ;
}
int tmp = hi; //特别注意
//查找该行
lo = ;
hi = col - ;
while (lo <= hi)
{
int mid = (lo+hi) / ;
if (matrix[tmp][mid] == target)
return true;
else if (matrix[tmp][mid] < target)
lo = mid + ;
else
hi = mid - ;
}
return false;
}
};

方法三:

class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target)
{
int row = matrix.size();
int col = matrix[].size(); if(row==||col==) return false;
if(matrix[][]>target||target>matrix[row-][col-]) return false; //加与不加都行 int lo=,hi=row*col-;
while(lo<=hi)
{
int mid=(lo+hi)/;
int i=mid/col;
int j=mid%col;
if(target==matrix[i][j])
return true;
else if(target>matrix[i][j])
lo=mid+;
else
hi=mid-;
}
return false;
}
};

[Leetcode] search a 2d matrix 搜索二维矩阵的更多相关文章

  1. 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 ...

  2. 074 Search a 2D Matrix 搜索二维矩阵

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行中的整数从左到右排序.    每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[  [1,   3, ...

  3. Leetcode74. Search a 2D Matrix搜索二维矩阵

    编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...

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

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

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

  6. LeetCode(74):搜索二维矩阵

    Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  7. search a 2D matrix(在二维数组中搜索一个元素)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. LeetCode OJ:Search a 2D Matrix(二维数组查找)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

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

随机推荐

  1. Spark-源码-SparkContext的初始化

    Spark版本 1.3SparkContext初始化流程 1.0 在我们的主类 main() 方法中经常会这么写 val conf = new SparkConf().setAppName(" ...

  2. JavaSE 第二次学习随笔(三)

    * 常见异常 * 数组越界异常 * 空指针异常 * * * 特点: 当程序出现异常的时候, 程序会打印异常信息并中断程序 * 所以当同时出现多个异常的时候只能执行第一个, 后边的用不到 * * 单异常 ...

  3. Leecode刷题之旅-C语言/python-21.合并两个有序链表

    /* * @lc app=leetcode.cn id=21 lang=c * * [21] 合并两个有序链表 * * https://leetcode-cn.com/problems/merge-t ...

  4. Python自动化运维——系统进程管理模块

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:psutil psutil是一个跨平台库,可以很轻松的为我们实现获取系统运行的进程和资源利用率等信息. 功能 ...

  5. RedHat7.1 安装Oracle12102

    选型: 32位的内存是个瓶颈,已经是64位的时代了.使用64位的CentOS6 和 64位的Oracle 11g R2 在虚拟机器安装,采用hostonly方式设置网络 注意:能上网的网卡要设置一下I ...

  6. app:showAsAction 和android:showAsAction

    app:showAsAction 它有三个可选项1.always:总是显示在界面上2.never:不显示在界面上,只让出现在右边的三个点中3.ifRoom:如果有位置才显示,不然就出现在右边的三个点中 ...

  7. 20145202马超《JAVA》预备作业1

    20145202马超<JAVA>预备作业1 你觉得自己专业吗?对专业的期望是什么? 我觉得自己很不专业,我对专业的期望:老师之前讲过德国的一个研究,学习分为5个档次,第三个档是能够自己发现 ...

  8. 开启一个项目如何上传到git

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...

  9. Close Java Auto Update in Windows 7 and Later

    0. Environment Windows 7JDK 1.6.0_45 1. Steps 1) Enter "JRE/bin" 2) Run javacpl.exe as adm ...

  10. python爬取数据需要注意的问题

    1 爬取https的网站或是接口的时候,如果是不受信用的SSL证书,会报错,需要添加如下代码,如下代码可以保证当前代码块内所有的请求都自动屏蔽ssl证书问题: import ssl # 这个是爬取ht ...