【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 ...
随机推荐
- Problem A: 选举 解题报告
Problem A: 选举 题意 给出一个投票过程.有\(n\)个选民和\(m\)个候选人,每个选民\(i\)有个不重且有序的可投集合\(\{a_i\}\). 对于第一轮投票,选民\(i\)会投给\( ...
- bzoj1345 序列问题 (贪心)
考虑某个点产生的贡献: 如果i左边是一个比它小的数x,那有两种情况: 1.x的左边的数y大于i,肯定要把x合并到i,i的贡献++ 2.x的左边的数y小于i,那肯定要把x合并到y,而这时候递归地来考虑, ...
- tomcat 性能调优
1. 内存 windows在bin/catalina.bat的注释下第一行加入 set JAVA_OPTS=-Xms2048m -Xmx2048m -Xss128K -XX:PermSize=64m ...
- haoi2006_受欢迎的牛_Solution
Brief Solution: 强连通tarjan+压缩点+判断是否除了一个点,其它点都有出度 Detailed Solution: 把牛看成点若一个点b能到达点a,则b认为a受欢迎若所有的点都能到达 ...
- 「Vue」watch基本用法
应用于监视路由地址改变,如有新地址(即路由地址改变)即执行自定义方法 methods: { itemShow() { this.$axios.get('item/item/'+this.id+'?to ...
- Java访问权限控制
访问权限控制 java提供了访问权限修饰词,以供类库开发人员向客户端程序员指明哪些是可用的,哪些是不可用的.访问权限控制的等级,从最大权限到最小权限依次是:public.prote ...
- SQL记录-Linux CentOS配置ORACLE 12c
1.准备LIINX软件包 操作系统:centos7 虚拟机:VMware 12 JDK:1.8 数据库:oracle 12c 2.配置基础环境 2.1 部署虚拟机VM(过程略) 2.2 部署操作系统C ...
- bzoj千题计划183:bzoj1197: [HNOI2006]花仙子的魔法
http://www.lydsy.com/JudgeOnline/problem.php?id=1197 题意转化:在n维空间中放m个n维球,问最多将空间分成几部分 f[i][j] 表示在i维空间中放 ...
- 何凯文每日一句打卡||DAY12
- 【原创】javascript模板引擎的简单实现
本来想把之前对artTemplate源码解析的注释放上来分享下,不过隔了一年,找不到了,只好把当时分析模板引擎原理后,自己尝试 写下的模板引擎与大家分享下,留个纪念,记得当时还对比了好几个模板引擎来着 ...