(LeetCode74)Search a 2D Matrix
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的更多相关文章
- 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 ...
- 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 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 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 ...
- 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 ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
随机推荐
- [Cocos2dx] CCCamera照相机 详解
前言 在3D游戏当中,我们经常会使用到照相机这个东西,无论你使用的是哪一款引擎,都会用到,同时,照相机这个东西涉及到的东西比较多,基础知识需要扎实一些才可以. 如何使用 很久之前做项目的时候用到过一次 ...
- hdu 4442 Physical Examination 贪心排序
Physical Examination Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 关于php一些问题
为什么说php是弱语言? 本身不严格区分变量的类型. 为什么说php是动态语言? 程序在运行时可以改变其结构.所谓的动态类型语言,意思就是类型的检查是在运行时做的. 为什么说php是脚本语言? 不需要 ...
- 反射生成SQL语句入门
今天我们来学习学习通过反射技术来生成SQL语句. 反射提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型.然后,可以调用类型的方法或访 ...
- 重温PHP之选择排序
思路:一组数中,选出最小者与第一个位置数交换,然后在剩余数中再找最小者与第二个位置数交换,依次类推,循环到倒数第二个数和最后一个数比较为止. 测试代码: 结果:
- Android - Mount a Samba share
Mount Manager, Cifs manager :Manage your CIFS/NFS network shares was working, but the command from t ...
- iOS中bundle的意义
什么是bundle? bundle就是一个文件夹,按照一定标准组织的目录结构.每个iOS APP至少有一个main bundle,这个main bundle包含了app的二进制代码及任何你用到的资源, ...
- 转:如何解决“My mac 64-bit”问题
童鞋们都知道Xcode会根据当前SDK在Run按钮旁边的选项栏中显示适合的Simulator供开发者选择,如下图: 但是有时候则错误显示“My mac 64-bit” ,这个明显不是我们想要的,如下图 ...
- andriod studio 获得程序名
getResources().getString(R.string.app_name)
- 【mysql】在mysql中更新字段的部分值,更新某个字符串字段的部分内容
在mysql中更新字段的部分值,更新某个字符串字段的部分内容 sql语句如下: update goods set img = REPLACE(img,'http://ozwm3lwui.bkt.clo ...