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 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.
【题目分析】
给定一个矩阵,满足矩阵中每一行都是递增的,而且每一行的第一个值比上一行的最后一个值要大,在这个矩阵中找到一个目标值。
【思路】
1. 最坏时间复杂度为O(m+n)的方法
分析矩阵我们发现矩阵有一个特点:它的每一行是递增的,而且每一列也是递增的。我们从矩阵的坐下角出发,如果目标值比当前值大,列索引就加1,如果目标值比当前值小,那么横索引就加1,直到搜索到目标值或者搜索完整个矩阵。例如我们要从如下矩阵中寻找目标值9:
<-----这个矩阵的每一行的第一个数并不比上一行最后一个数大,但是这个算法依旧可以应用在本题目。
我们从18出发,绿色线条显示了我们的移动轨迹。可以很容易看出,如果目标值为右上角的元素,则需要搜索最长的步数。
2. 时间复杂度为log(m) + log(n)的算法
很明显我们可以采用二分查找算法查找第一列,定位出目标值位于哪一行,然后在该行再采用二分查找具体确定目标值的位置。
【思路1代码】
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null) return false;
int i = matrix.length - 1;
int j = 0;
while(i >= 0 && j < matrix[0].length){
if(target > matrix[i][j]) j++;
else if(target < matrix[i][j]) i--;
else return true;
}
return false;
}
}
LeetCode OJ 74. Search a 2D Matrix的更多相关文章
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 【一天一道LeetCode】#74. Search a 2D Matrix
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode OJ:Search a 2D Matrix(二维数组查找)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 【转载】pygame安装与两种版本的Python兼容问题
在开始学习游戏编程之前,我们先来安装下pygame和python3.2.5 参考园友: http://www.cnblogs.com/hongten/p/hongten_pygame_install. ...
- Linux的一些简单命令(四)-用户和组账户管理
linux操作系统是一个多用户操作系统,它允许多用户同时登录到系统上并使用资源.系统会根据账户来区分每个用户的文件,进程,任务和工作环境,使得每个用户工作都不受干扰 1.保存用户信息的文件:/etc/ ...
- CodeForces 669C Little Artem and Matrix GNU
模拟. 把操作记录一下,倒着复原回去. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cs ...
- Python学习笔记——基础篇【第四周】——迭代器&生成器、装饰器、递归、算法、正则表达式
目录 1.迭代器&生成器 2.装饰器 a.基本装饰器 b.多参数装饰器 3.递归 4.算法基础:二分查找.二维数组转换 5.正则表达式 6.常用模块学习 #作业:计算器开发 a.实现加减成熟及 ...
- IntentService和Service的区别
整个看下来是一个Service+Thread+handle的结合体, Service:比Activity的被kill的级别低 Thread:不阻塞UI线程 Handle:队列式的消息循环 那这个玩意的 ...
- rocketmq(1)
参考: 开源社区:https://github.com/alibaba/RocketMQ rocketmq入门: http://www.cnblogs.com/LifeOnCode/p/4805953 ...
- C#第十二天
1.Directory 操作文件夹 1)创建文件夹 Directory.CreateDirectory(@"C:\a"); Console.WriteLine("创建成功 ...
- 我的学习记录JAVA第二天
- date格式化
Linux: [ghsea@localhost ~]date +%Y:%m:%d [ghsea@localhost ~]date +%Y-%m%d [ghsea@localhost ~]date +% ...
- iOS 导航栏 不透明
UINavigationBar.appearance().translucent = false UINavigationBar.appearance().barStyle = UIBarStyle. ...