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 ...
随机推荐
- IIS错误解决办法(HTTP Error 500.19 - Internal Server Error)
window10 切换登陆用户,VS2015的IIS Express 调试代码报HTTP Error 500.19 - Internal Server Error 错误,无法读取配置文件解决办法. I ...
- jquery验证表单是否满足正则表达式是否通过验证例子
//验证通用函数 a表示元素对象,b表示正则表达式,c存bool值 function testyz(a,b,c){ c=false; $(a).on("blur",function ...
- css基础和心得(四)
现在来说相对定位: 如果想为元素设置层模型中的相对定位,需要设置position:relaive(表示相对 定位),它通过left.right.top.bottom属性确定元素在正常文档流中便宜位 ...
- js-数据转换
<script type="text/javascript"> var msg = '{"code": 0, "data": 2 ...
- scale-free network
原文链接:http://lihailian.bokee.com/6013647.html 1.什么是无尺度现象? 统计物理学家习惯于把服从幂次分布的现象称为无尺度现象. 在做大量统计实验之前,科学家预 ...
- 5、范围标签<fieldset></fieldset>
<fieldset style="border:0;border:1px solid red;"> <legend style="background- ...
- CSS display:inline-block的元素特点:
将对象呈递为内联对象,但是对象的内容作为块对象呈递.旁边的内联对象会被呈递在同一行内,允许空格. 在CSS中,块级对象元素会单独占一行显示,多个block元素会各自新起一行,并且可以设置width,h ...
- NET 2.0(C#)调用ffmpeg处理视频的方法
另外:ffmpeg的net封装库 http://www.intuitive.sk/fflib/ NET 2.0 调用FFMPEG,并异步读取输出信息的代码...public void ConvertV ...
- springmvc+maven
http://blog.csdn.net/zht666/article/details/8673609/
- non-ARC代码转 ARC 排除 “Existing instance variable 'delegate' for property with assign attribute must be _unsafe _unretained” 错误
原来非ARC代码是 @interface MHWebImageDownloader : NSObject { id<MHWebImageDownloaderDelegate> delega ...