(LeetCode74)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.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
题目:
在一个m*n二维数组中,每一行从左到右递增,每一行的第一个元素比上一行最后一个元素大。
判断某个元素是否在 该数组中。
思路:
问题隐含的一个信息就是:每一列也从上到下递增。
方法1:
将二维数组按行展开的话,就是一个排序的一维数组,因此通过一维数组的二分查找很容易得到答案。
方法2:
鉴于数组的规律性,选取数组查找范围的右上角数字,如果与查找的数字相等, 则返回true,如果比查找的数字大,则将该数字所在列从查找范围剔除,如果比查找数字小,则将该数字所在行从查找范围中剔除。
方法3:
先通过二分查找元素所在的行,再在所在行通过二分查找元素。
代码:
1、一维数组方法:
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int rows=matrix.size();
int cols=matrix[].size();
int left=,right=(rows*cols-);
int mid,r,c,val;
while(left<=right){
mid=left+((right-left)>>);
r=mid/cols;
c=mid%cols;
if(matrix[r][c]==target)
return true;
if(matrix[r][c]<target)
left=mid+;
else
right=mid-;
}
return false;
}
};
2、右上角元素比较方法
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int rows=matrix.size();
int cols=matrix[].size();
int r=,c=cols-;
while(r<rows && c>=){
if(target==matrix[r][c])
return true;
if(target<matrix[r][c])
c--;
else
r++;
}
return false;
}
};
3、二分查找行方法
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int rows=matrix.size();
int cols=matrix[].size();
int lRow=,rRow=rows-;
int midRow;
while(lRow<=rRow){
midRow=lRow+((rRow-lRow)>>);
if(matrix[midRow][]==target || matrix[midRow][cols-]==target)
return true;
if(matrix[midRow][]<target && matrix[midRow][cols-]>target)
break;
if(matrix[midRow][]>target)
rRow=midRow-;
else
lRow=midRow+;
}
if(lRow<=rRow){
int lCol=,rCol=cols-;
int midCol;
while(lCol<=rCol){
midCol=lCol+((rCol-lCol)>>);
if(matrix[midRow][midCol]==target)
return true;
if(matrix[midRow][midCol]>target)
rCol=midCol-;
else
lCol=midCol+;
}
}
return false;
}
};
(LeetCode74)Search a 2D Matrix的更多相关文章
- 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 fo ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- Search a 2D Matrix | & II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...
- 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 ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
随机推荐
- Alpha 冲刺报告8
组长:吴晓晖 今天完成了哪些任务: maven和idea用的不熟啊,jar包或者war包导出来一直有问题:生气了把ide扔到服务器里去运行springboot了,卡哭了,终于可以运行了,然后debug ...
- bzoj 2178
这题调精度真痛苦啊(向管理员要了数据才调出来). 用的是hwd在WC2015上讲的方法,考虑将原图分割,根据每个圆的左右边界和圆与圆交点的横坐标来分割,这样原图就被分成很多竖着的长条,并且每一条中间都 ...
- 简单的php自定义错误日志
平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的<最佳日志实践>觉得写一个清晰明了,结构分明的日志还是非常有必要的. 在写日志前,我们问问自己:为什么我们有时要记录自定义 ...
- 深入理解RESTful Web Services
RESTful的软件架构已经多火不用多说,和MVC架构一样,很多网站服务(Web Services)都遵循RESTful设计模式,那么到底什么是RESTful Web Services呢?设计一个RE ...
- Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举
B. Covered Path Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...
- mysql数据库cup飙升处理思路
1.先top查看是那一个进程,哪个端口占用CPU多. 2.show processeslist查看是否由于大量并发,锁引起的负载问题. 3.否则,查看慢查询,找出执行时间长的sql:explain分析 ...
- 自动化运维工具 ~puppet~
一.模板的应用 到目前为止,资源申报.定义类.声明类等所有功能都只能一个manifest文件中实现,但这却非有效的基于puppet管理IT资源架构的方式.实践 中,一般需要把manifest文件分解成 ...
- Go语言Web框架gwk介绍 (四)
事件 gwk支持事件系统,但并没有硬编码有哪些事件,而是采用了比较松散的定义方式. 订阅事件有两种方式: 调用On函数或者OnFunc函数 func On(moudle, name string, h ...
- javascript小记-闭包理解
这几天也在看一些javascript的知识,算是对以往的一个复习,现小记一下,方便以后查询. 相信大家在研究javascript的高级特性的时候,肯定会遇到闭包的概念,自己在各种复习资料中,也发现了不 ...
- ios 得到目录大小 进率是1000
- (CGFloat)folderSizeAtPath:(NSString *) folderPath { NSFileManager * manager = [NSFileManager d ...