[Leetcode] 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, 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 搜索二维矩阵的更多相关文章
- 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 ...
- 074 Search a 2D Matrix 搜索二维矩阵
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[ [1, 3, ...
- Leetcode74. Search a 2D Matrix搜索二维矩阵
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [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):搜索二维矩阵
Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 ...
- 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 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 ...
- [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 ...
随机推荐
- HTML基础实例
本节列举了一些简单的HTML例子,帮助大家更感性地认识HTML标签.是不是对一些标签还不熟悉?别担心,后面几个章节会有详细说明,先跑几个例子看看效果吧. HTML文档相关标签所有HTML文档必须以&l ...
- angularjs Directive自定义指令详解
作用:需要用Directive有下面的情景: 1.使你的Html更具语义化,不需要深入研究代码和逻辑即可知道页面的大致逻辑. 2. 抽象一个自定义组件,在其他地方进行重用. 3.使用公共代码,减少重复 ...
- Asp.NET Core 在IIS部署 An assembly specified in the application dependencies manifest was not found
今天在发布应用的时候,出来了一个报错:An assembly specified in the application dependencies manifest was not found 情况如下 ...
- 百度收录检测并主动推送API(实时 mip推送通用)
简要描述: 百度收录检测并主动推送API(实时) 请求URL: api.bigjiji.com/baiduCheck_123456 调用方式: img标签 参数: 参数名 必选 类型 说明 site ...
- lnmp+phpmyadmin+laravel 环境配置
腾讯云 Ubuntu16.04 添加用户 useradd 与 adduser Ubuntu下useradd不会在/home下自动创建与用户名同名的用户目录,而且不会自动选择shell版本,也没有设置密 ...
- Learning Experience of Big Data: Learn to install CentOs 6.5 on my laptop
I have learnt some experience about Big Data during my summer vocation,I was told that The first thi ...
- PADS快捷键
问:在pads layout中怎样显示或隐藏铺铜的效果 答: 无模命令:po 或者spo 前者是平面层 后者是混合层. 同时你可以在ctrl+alt+c 色彩项中关闭 copper . 使用 无模命令 ...
- 003---random随机数模块
import random # 随机数(0-1) print(random.random()) # 随机整数, 包含尾巴 print(random.randint(-1, 2)) # 不包含尾巴 pr ...
- 使用fiddler和jmeter进行简单的接口测试。
初学接口测试,以下内容是记录首次使用fiddler和jmeter进行接口测试的步骤,可能步骤有点繁琐,如果有不对的地方,欢迎大家指正. 准备活动: 1.打开fiddler,打开fiddler以后会自动 ...
- Linux-获得命令帮助man
date:显示当前系统时间,修改时间 clock,hwclock:显示硬件时间 cal:calendar,查看日历 计时器靠晶体振荡器来完成计时 Linux: 实时时钟,rtc,real time c ...