【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 ...
随机推荐
- 为harbor部署swagger-ui
有了swagger,所有REST API就一览无余了. Harbor官方已经准备好了部署脚本,部署起来其实特别简单,这里仅作记录. 1.下载部署脚本和swagger文件到本地harbor目录. # c ...
- (转)Maven学习总结(五)——聚合与继承
孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(五)——聚合与继承 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 1 <mod ...
- 浅谈移动端 View 的显示过程
作者:个推安卓开发工程师 一七 随着科技的发展,各种移动端早已成为人们日常生活中不可或缺的部分,人们使用移动端产品工作.社交.娱乐……移动端界面的流畅性已经成为影响用户体验的重要因素之一.那么你是否思 ...
- Docker下的Spring Cloud三部曲之一:极速体验
版权声明:欢迎转载,请注明出处,谢谢. http://blog.csdn.net/boling_cavalry/article/details/79177930 目录(?)[+] 从本章开始, ...
- 位运算符和unity Layers
按位运算符:与(&).非(~).或(|).异或(^).<<(左移).>>(右移).位运算符主要用来对二进制位进行操作. 逻辑运算符:&&.||.!.逻辑 ...
- Python的常用内置函数介绍
Python的常用内置函数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.取绝对值(abs) #!/usr/bin/env python #_*_coding:utf-8_ ...
- python命名空间与闭包函数详解
python命名空间与闭包函数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要介绍的知识点如下: 1>.三元运算 2>.命名空间 3>.globa ...
- 鸟哥的Linux私房菜——第十三章:Vim编译器
视频链接: 土豆: B站:http://www.bilibili.com/video/av9891085/ 本章的细节还是挺多的,可是我懒啊~~
- Linux命令dd与cp的区别
原文链接:http://blog.csdn.net/erazy0/article/details/6087554 问:看了一些关于dd和cp的命令,但是我始终无法明白dd和cp之间有什么不同?不是都可 ...
- Spring RedisTemplate操作-Set操作(5)
@Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; ...