[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 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
类似题目:
参考资料:
https://leetcode.com/problems/search-a-2d-matrix/
https://leetcode.com/problems/search-a-2d-matrix/discuss/26292/Java-clear-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵的更多相关文章
- [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] 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] 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] 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 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [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 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 ...
随机推荐
- 单个视频播放控制&默认横屏播放
一.视频列表中控制只允许一个视频播放 // 获取DOM中所有的video标签 var videoTags = document.querySelectorAll('video'); // 控制播放的视 ...
- 【转】Git GUI基本操作
一.Git GUI基本操作 1.版本库初始化 gitpractise文件夹就变成了Git可以管理的仓库,目录下多了一个.git文件夹,此目录是Git用于管理版本库的,不要擅自改动里面的文件,这样会破坏 ...
- 生成 Visual Studio 中的代码的文档生成神器
当我们在团队开发中的时候,经常要给别人提供文档,有了这个工具,设置一下,一键生成.前提是你要写好xml注释. 这也是开源项目: https://sandcastle.codeplex.com/ 它就是 ...
- Filebeat和pipleline processor-不部署logstash,实现对数据的处理
利用ingest node所提供的Pipeline帮我们对数据进行处理. 在Elasticsearch中的配置文件elasticsearch.yml文件中配置:node.ingest: true in ...
- 使用AvalonEdit实现WPF的Lua编辑器
原文发布于:https://www.chenxublog.com/2019/07/14/use-avalonedit-make-wpf-lua-editor.html 由于LLCOM里面内置了Lua代 ...
- Ansible Jinja2 模板
1.jinja2渲染NginxProxy配置文件 jinja2 房屋建筑设计固定的? jinja2模板与Ansible关系 Ansible如何使用jinja2模板 template模块 拷贝文件? t ...
- HTML5中localStorage的使用
为什么要存在localStorage 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cookie中每条cook ...
- 场sharepoint2016数据库恢复站点
前不久公司support方,不小心把IIS的应用删除了,算是灼急了,不过有过原来恢复的经历,似乎有了心理准备,可是这次比上次严重些.技术操作复杂些,不过通过此事,也是进一步了解了SP2016数据库结构 ...
- Python项目中使用配置文件
一些讨论 Python中使用配置文件的最佳实践 Python中使用配置文件的最好方法 Python符号常量 多种配置文件方案对比 我的建议 1. 排除yaml yaml 不是一个好主意, 因为需要给项 ...
- CSS自定义右键菜单
<style> .menu { width: 100px; font-size: 14px; font-family: "微软雅黑"; border: 1px soli ...