【LeetCode】74. Search a 2D Matrix
Difficulty:medium
More:【目录】LeetCode Java实现
Description
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.
Example 1:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true
Example 2:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false
Intuition
regard the matrix as an array, and then use binary search.
matrix[x][y]=array[x*cols+y]
array[m]=matrix[m/cols][m%cols]
Solution
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix==null || matrix.length<=0 || matrix[0].length<=0)
return false;
int rows=matrix.length; //行数
int cols=matrix[0].length; //列数
int low=0;
int high=rows*cols-1;
while(low<=high){
int mid=(low+high)/2;
if(matrix[mid/cols][mid%cols]==target){
return true;
}else if(matrix[mid/cols][mid%cols]>target){
high=mid-1;
}else if(matrix[mid/cols][mid%cols]<target){
low=mid+1;
}
}
return false;
}
Complexity
Time complexity : O(log(n*m))
Space complexity : O(1)
What I've learned
1. Ought to have a good command of the change between array <=> matrix, especially the change of their indexes
More:【目录】LeetCode Java实现
【LeetCode】74. Search a 2D Matrix的更多相关文章
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】240. 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. Thi ...
- 【一天一道LeetCode】#74. Search a 2D Matrix
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【刷题-LeetCode】240. 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. Thi ...
- LeetCode OJ 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 ...
- 【Lintcode】038.Search a 2D Matrix II
题目: Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence ...
- 【Lintcode】028.Search a 2D Matrix
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
随机推荐
- STM32配置GPIO前须先打开其时钟,否则配置失败
@2018-5-9 17:11:38 STM32配置GPIO前须先打开其时钟,否则配置失败
- 动态dp 板子
动态dp 瞎扯两句吧 先从序列上理解,维护链的最大独立集. 考虑是从左边转移的,那么矩阵的转移唯一,直接放在线段树上就可以了. 放在树上的话,儿子都可以转移,把轻儿子的转移放在子链链头更新,然后每条链 ...
- 洛谷 P1310 表达式的值 解题报告
P1310 表达式的值 题目描述 对于1 位二进制变量定义两种运算: 运算的优先级是: 先计算括号内的,再计算括号外的. "× "运算优先于"⊕"运算,即计算表 ...
- 【HDU1710】树的遍历
题目大意:给定一棵 N 个节点的二叉树的前序遍历和中序遍历,求其后序遍历. 题解:递归操作,每次只需知道先序遍历和中序遍历的开始点,左子树大小即可,根据前序遍历的开始位置可知子树根节点的坐标,再在中序 ...
- (转)Maven学习总结(八)——使用Maven构建多模块项目
孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(八)——使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为doma ...
- (转)每天一个linux命令(9):touch 命令
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1 基本使用 1.命令格式: touch [选项]... 文件... 2.命令参数 ...
- Linux上查找
locate 用法:locate filename locate是Linux系统中的一个查找(定位)文件命令,和find命令等找寻文件的工作原理类似,但locate是通过生成一个文件和文件夹的索引数据 ...
- SQL Server 执行计划的理解
要理解执行计划,怎么也得先理解,那各种各样的名词吧.鉴于自己还不是很了解.本文打算作为只写懂的,不懂的懂了才写. 在开头要先说明,第一次看执行计划要注意,SQL Server的执行计划是从右向左看的. ...
- JS原型继承与类的继承
我们先看JS类的继承 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- <meta content='IE=edge,chrome=1' http-equiv='X-UA-Compatible' />
代码一:<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> http-eq ...