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.

Example 1:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true

Example 2:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false

这道题要求搜索一个二维矩阵,由于给的矩阵是有序的,所以很自然的想到要用二分查找法,可以在第一列上先用一次二分查找法找到目标值所在的行的位置,然后在该行上再用一次二分查找法来找是否存在目标值。对于第一个二分查找,由于第一列的数中可能没有 target 值,该如何查找呢,是博主之前的总结帖 LeetCode Binary Search Summary 二分搜索法小结 中的哪一类呢?如果是查找第一个不小于目标值的数,当 target 在第一列时,会返回 target 所在的行,但若 target 不在的话,有可能会返回下一行,不好统一。所以可以查找第一个大于目标值的数,也就是总结帖中的第三类,这样只要回退一个,就一定是 target 所在的行。但需要注意的一点是,如果返回的是0,就不能回退了,以免越界,记得要判断一下。找到了 target 所在的行数,就可以再次使用二分搜索,此时就是总结帖中的第一类了,查找和 target 值相同的数,也是最简单的一类,分分钟搞定即可,参见代码如下:

解法一:

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty() || matrix[].empty()) return false;
int left = , right = matrix.size();
while (left < right) {
int mid = (left + right) / ;
if (matrix[mid][] == target) return true;
if (matrix[mid][] <= target) left = mid + ;
else right = mid;
}
int tmp = (right > ) ? (right - ) : right;
left = ;
right = matrix[tmp].size();
while (left < right) {
int mid = (left + right) / ;
if (matrix[tmp][mid] == target) return true;
if (matrix[tmp][mid] < target) left = mid + ;
else right = mid;
}
return false;
}
};

当然这道题也可以使用一次二分查找法,如果我们按S型遍历该二维数组,可以得到一个有序的一维数组,只需要用一次二分查找法,而关键就在于坐标的转换,如何把二维坐标和一维坐标转换是关键点,把一个长度为n的一维数组转化为 m*n 的二维数组 (m*n = n)后,那么原一维数组中下标为i的元素将出现在二维数组中的 [i/n][i%n] 的位置,有了这一点,代码很好写出来了:

解法二:

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty() || matrix[].empty()) return false;
int m = matrix.size(), n = matrix[].size();
int left = , right = m * n;
while (left < right) {
int mid = (left + right) / ;
if (matrix[mid / n][mid % n] == target) return true;
if (matrix[mid / n][mid % n] < target) left = mid + ;
else right = mid;
}
return false;
}
};

这道题其实也可以不用二分搜索法,直接使用双指针也是可以的,i指向0,j指向列数,这样第一个被验证的数就是二维数组右上角的数字,假如这个数字等于 target,直接返回 true;若大于 target,说明要减小数字,则列数j自减1;若小于 target,说明要增加数字,行数i自增1。若 while 循环退出了还是没找到 target,直接返回 false 即可,参见代码如下:

解法三:

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty() || matrix[].empty()) return false;
int i = , j = (int)matrix[].size() - ;
while (i < matrix.size() && j >= ) {
if (matrix[i][j] == target) return true;
else if (matrix[i][j] > target) --j;
else ++i;
}
return false;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/74

类似题目:

Search a 2D Matrix II

参考资料:

https://leetcode.com/problems/search-a-2d-matrix/

https://leetcode.com/problems/search-a-2d-matrix/discuss/26292/Java-clear-solution

https://leetcode.com/problems/search-a-2d-matrix/discuss/26220/Don't-treat-it-as-a-2D-matrix-just-treat-it-as-a-sorted-list

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Search a 2D Matrix 搜索一个二维矩阵的更多相关文章

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

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

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

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

  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 Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  7. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

  8. LeetCode: Search a 2D Matrix 解题报告

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  9. [CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组

    13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the n ...

随机推荐

  1. ASP.NET Core 中文文档 第二章 指南(4.1)ASP.NET Core MVC 与 Visual Studio 入门

    原文:Getting started with ASP.NET Core MVC and Visual Studio 作者:Rick Anderson 翻译:娄宇(Lyrics) 校对:刘怡(Alex ...

  2. ArcGIS属性选择器筛选

    以前我也会过,后来忘得干干净净.还是老话,学习新东西不难,难的是不断总结和提升.重新学习ArcGIS的属性筛选功能,记录如下. 要求: 1. 查找,删除. 例:根据属性当中相同字段,选择符合要求的数据 ...

  3. JDBC_part2_DML以及预编译_编写DBUtil工具类

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! jdbc day02 DML语法 比起插叙语句,没有R ...

  4. 一个java文件中可包含多个main方法

    java中的main方法是java应用程序的入口,java程序在运行时,首先调用执行main方法.但并不是说java中只能有一个main方法,不同类中都可以包含main方法.当JVM进行编译时,会提示 ...

  5. java代码解压zip文件

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...

  6. office 2010 word每次启动都需要配置

    解决方式: 进入cmd,运行以下命令即可,如果提示已存在,选择Y覆盖就行了 reg add HKCU\Software\Microsoft\Office\14.0\Word\Options /v No ...

  7. ArcGIS Engine开发前基础知识(2)

    ArcGIS基本控件简介 ArcGIS Engine控件是一组可视化的开发组件,每个ArcGIS Engine控件都是一个COM组件.这些组件包括MapControl,PageLayoutContro ...

  8. 可空类型(Nullable<T>)及其引出的关于explicit、implicit的使用

    问题一:Nullable<T>可赋值为null 先看两行C#代码 int? i1 = null; int? i2 = new int?(); int? 即Nullable<int&g ...

  9. 关于Android中的三级缓存

    三级缓存的提出就是为了提升用户体验.当我们第一次打开应用获取图片时,先到网络去下载图片,然后依次存入内存缓存,磁盘缓存,当我们再一次需要用到刚才下载的这张图片时,就不需要再重复的到网络上去下载,直接可 ...

  10. Windows Server 2003安装方法

    1. 设置BIOS,从光驱引导启动.光盘放入光驱,自动读盘,选择第一项“安装Windows Server 2003,Enterprise Bdition”: 2. 弹出加载安装文件界面. 3. 出现安 ...