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的更多相关文章

  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 fo ...

  2. 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 ...

  3. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  4. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  5. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  7. 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 ...

  8. 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 ...

  9. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

随机推荐

  1. BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆

    https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...

  2. [CC-SUBWAY]Subway Ride

    [CC-SUBWAY]Subway Ride 题目大意: 一棵\(n(n\le5\times10^5)\)个点的含重边的树,总边数为\(m(m\le10^6)\),每条边有一个颜色.\(q(q\le5 ...

  3. PyQt QString 与 Python str&unicode

    昨日,将许久以前做的模拟网页登录脚本用PyQt封装了一下,结果出大问题了, 登录无数次都提示登录失败!!而不用PyQt实现的GUI登录直接脚本登录无数次都提示登录成功!!心中甚是伤痛,于是探究起来,解 ...

  4. Codeforces Round #357 (Div. 2) E. Runaway to a Shadow 计算几何

    E. Runaway to a Shadow 题目连接: http://www.codeforces.com/contest/681/problem/E Description Dima is liv ...

  5. 自动化运维之 ~cobbler~

    一 .Cobbler简介 Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows.该工具使用python开发,小巧轻便(才15k 行python代码),使用简 ...

  6. 小程序navigator点击有时候会闪一下

    <navigator hover-class="none">

  7. Linux下生成随机密码(转)

    1.使用SHA算法来加密日期,并输出结果的前32个字符: date +%s |sha256sum |base64 |head -c 32 ;echo 生成结果如下: ZTNiMGM0NDI5OGZjM ...

  8. 使用Lazy<T>实现对客户订单的延迟加载

    "延迟加载"是指在需要的时候再加载数据.比如获得一个Customer信息,并不会把该Customer的Orders信息一下加载出来,当需要显示Orders的时候再加载.简单来说,就 ...

  9. 使用C#中的ref关键字,用2个简单例子来说明

    在C#中,如果在方法参数前面加上ref关键字,说明参数传递的是引用,而不是值.如何理解呢? 参数是简单类型的例子 static void Main(string[] args) { string te ...

  10. java string常见操作题

    1. 每个基本类型封装类都有将string转换为基本数据类型的方法 对于非常大的数字请使用Long,代码如下 int age = Integer.parseInt("10");  ...