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. QQ群技术:0成本创建2000人QQ群技巧

    群人数,直接关系群权重;于排名,意义非凡;此法靠谱,笔者亲测. 就说这张图,这类关键词,要是没2000人群,不管你多流弊,你是做不上去滴. 于QQ群霸屏,笔者有太多的笔墨,各种排名技巧,阿力推推早前明 ...

  2. hive的desc命令

    desc命令 desc 命令是为了展示hive表格的内在属性.例如列名,data_type,存储位置等信息.这个命令常常用在我们对hive表格观察之时,我们想要知道这个hive各个列名(基于这些具体列 ...

  3. C语言实例解析精粹学习笔记——35(报数游戏)

    实例35: 设由n个人站成一圈,分别被编号1,2,3,4,……,n.第一个人从1开始报数,每报数位m的人被从圈中推测,其后的人再次从1开始报数,重复上述过程,直至所有人都从圈中退出. 实例解析: 用链 ...

  4. Druid时序数据库常见问题及处理方式

    最近将Druid-0.10.0升级到Druid-0.12.1的过程中遇到一些问题,为了后期方便分析问题和及时解决问题,特此写这篇文章将工作中遇到的Druid问题及解决办法记录下来,以供其他人借鉴,其中 ...

  5. java加密用到了BASE64Decoder时报错信息:Access restriction: The type BASE64Encoder is not accessible due to restrict

    在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: Access restriction : ...

  6. 4-oracle11g安装

    1.导入安装包,解压 [root@ocp Desktop]# unzip p10404530_112030_Linux-x86-64_1of7.zip [root@ocp Desktop]# unzi ...

  7. ajax设置自定义头

    一.setting参数 headers $.ajax({ headers: {        Accept: "application/json; charset=utf-8"  ...

  8. centos linux 因别名问题引起的麻烦及解决技巧

    老男孩儿-19期 L005-13节中分享.自己整理后发到自己微博中留档. 原文:http://oldboy.blog.51cto.com/2561410/699046 实例:老男孩linux实战培训第 ...

  9. oracle 认识

    有一家叫甲骨文的粮店,老板很严谨,为了防止仓库的粮食在买入卖出的时候发生问题,他制订一套流程,首先进出仓库的每一旦粮食都要求有一个编号(SCN),而且出入库之前必须先放到一个平台上(buffer ca ...

  10. C#文件重命名的代码

    C#中没有重命名的方法,自己写了一个方法,来处理文件的重命名. /// <summary> /// 重命名文件夹内的所有子文件夹 /// </summary> /// < ...