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. 搞笑入群二维码在线生成源码 php图片合成并添加文字水印

    在凤凰网看到一篇文章:微信群二维码也能“整人”,99%的好友会中招!感觉挺好玩,所以自己也想做一个! 冷静分析

  2. Ubuntu装完后要做的几件事

    Ubuntu装完后要做的几件事 改hosts 无论哪里,改hosts都是第一件事,没hosts咋google.没google咋活.在终端输入命令 sudo gedit /etc/hosts在# The ...

  3. labview在编写程序框图中遇到的一些布尔按钮控制布尔指示灯问题

                      上图布尔控件按下,数据0x04成功发送给下位机,布尔灯不亮. ............... ............. ........... 下图布尔控件按下, ...

  4. 博科brocade光纤交换机alias-zone的划分-->实操案例

    一,图形化操作 光纤交换机作为SAN网络的重要组成部分,在日常应用中非常普遍,本次将以常用的博科交换机介绍基本的配置方法. 博科300实物图: 环境描述: 如上图,四台服务器通过各自的双HBA卡连接至 ...

  5. vs2013中将复制过来的文件或文件夹显示到解决方案管理

    先将文件夹和文件复制到VS程序所在的位置,在VS2013解决方案资源管理器中找到这些文件所在的上一级文件夹,先将那个上层文件夹收缩起来,然后再点击解决方案资源管理器上的“显示所有文件”按纽,展开这个文 ...

  6. netty学习记录2

    昨天晚上在看到7.2章MessagePack编码器和解码器开发这一章时,书里面没有贴出全部的代码,然后我按照我自己的想法把代码补全后,发现死活没有把代码跑通. 然后花了挺多时间在网上找,很多博客都贴出 ...

  7. jmeter插件之jsonpath提取响应结果和做断言

    准备工作: 1. jmeter3.X已经自带了提取响应结果的插件:JSON Extractor 2. 下载断言插件:https://jmeter-plugins.org/wiki/JSONPathAs ...

  8. YAGNI 声明

    1.YAGNI介绍 YAGNI 全名是 You aren't Going to Need It,在你设计草案的初稿中,应该努力使用最简单可以工作的事物,直至程序的某个方面要求你添加额外的特性. 2.思 ...

  9. 第二篇 Fiddler配置_浏览器&手机

    什么是Fiddler? 网络项目的开发和测试中,Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的 ,可以说是非常常用的手头工具了,本文就Fiddler使用和配置进行说明. ...

  10. 【Java】Map转换器

    描述: 在控制层接收参数时候, 往往会出现Json格式需要转换为Bean. 通常一两个字段可以用new去save pojo, 但字段多的情况呢? 以下就是为了解决这个尴尬情况,  自己写一个转换工具类 ...