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. Git 初学者使用指南及Git 资源整理

    Git 资源整理 Git is a free and open source distributed version control system designed to handle everyth ...

  2. bzoj 1312 最大密度子图

    晕,m=0是要输出1(弄的我还找管理员要数据,但明显题意是叫我们输出0呀) 最大密度子图,把边转换成点,然后二分答案,跑最大权闭合子图判定是否可行. #include <cstdio> # ...

  3. 华为S5300系列交换机V200R001SPH027升级补丁

    S5300SI-V200R001SPH027.pat 附件: 链接:https://pan.baidu.com/s/1ulE0j5Rp4xMkAaOjNGKLMA  密码:d5ze

  4. WPF中删除打开过的图片

    在WPF中,当我们删除打开过的图片时,往往会遇到"...无法删除,文件正在被另一个进程使用"的异常.即使当前文件是打开后关闭过的也不行. 这个问题的原因很简单,是因为WPF的缓存策 ...

  5. android aapt 用法 -- ApkReader

    aapt 是android assert packaging tool的缩写,具体如下: 1. 列出apk包的内容 aapt l[ist] [-v] [-a] file.{zip,jar,apk} - ...

  6. 【原】移动web资源整理(安卓、ios移动端兼容性问题归整)

     meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wi ...

  7. 委托、多播委托、泛型委托Func,Action,Predicate,ExpressionTree

    当试图通过一个事件触发多个方法,抽象出泛型行为的时候,或许可以考虑使用委托.     通过委托构造函数或委托变量把方法赋值给委托 private delegate double DiscountDel ...

  8. MVC二级联动使用$.ajax方法获取后端返回的字符串

    在"MVC二级联动使用$.getJSON方法"中使用$.getJSON()获取后端返回的JSon. 本篇使用jQuery的$.ajax()获取后端返回的字符串,实现二级联动.   ...

  9. JSONObject以及json(转)

    一.JAR包简介      要使程序 可以运行 必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包:      1.commons-lang.jar      2.commons- ...

  10. JDK1.6官方下载_JDK6官方下载

    JDK1.6官方下载_JDK6官方下载地址: http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin ...