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. 51nod 1515 明辨是非 启发式合并

    1515 明辨是非 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 Description 给n组操 ...

  2. POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9050   Accepted: 3604 Descr ...

  3. opencv hog算子

    梯度直方图特征(HOG) 是一种对图像局部重叠区域的密集型描述符, 它通过计算局部区域的梯度方向直方图来构成特征.Hog特征结合SVM分类器已经被广泛应用于图像识别中,尤其在行人检测中获得了极大的成功 ...

  4. leetcode659. Split Array into Consecutive Subsequences

    leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...

  5. Qt线程外使用Sleep

    一:方法1 QTime t; t.start(); while(t.elapsed()<1000){     QCoreApplication::processEvents();} 二:方法2 ...

  6. Git_多人协作

    当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信息,用git remote: $ git r ...

  7. Bootstrap_CSS概览

    在这一章中,我们将讲解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML 5 文档类型(Doctype) Bootstrap 使用了一些 H ...

  8. Knockout官网实例在MVC下的实现-02,实现计次

    本篇使用Knockout在MVC下实现"Hello World",对应的官网实例在这里. 当次数达到3: View视图 页面包含三个部分:1.显示点击按钮的次数2.button按钮 ...

  9. 在IOS 模拟器中 输入中文

    模拟器默认的配置种没有“小地球”,只能输入英文.加入中文方法如下: 找到模拟器的Settings--->General-->Keyboard-->International KeyB ...

  10. Announcing Mobile SDK V2.0

    As you might have read over at our PayPal Forward Blog it’s time to celebrate for PayPal | Developer ...