LeetCode——Search a 2D Matrix II
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 in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5
, return true
.
Given target = 20
, return false
.
首先想到的就是遍历整个矩阵,时间复杂度是O(m*n),肯定是Timeout。
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) { for(int i=0; i<matrix.length; i++) {
for(int j=0; j<matrix[i].length; j++) {
if(matrix[i][j] == target) {
return true;
}
}
} return false;
}
}
然后在优化的话就想到了二分。对每一行进行二分。时间复杂度是O(n*logm),还是Timeout,二分用的越多时间复杂度就越高,所以行列都二分(有点递归分治的意思)更会Timeout。
public class Solution { public boolean binarySearch(int[] arr, int terget) { int left = 0, int right = arr.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(target == arr[mid]) {
return true;
}
if(target > arr[mid]) {
left = mid + 1;
}
else {
right = mid - 1;
}
} return false;
} public boolean searchMatrix(int[][] matrix, int target) { for(int i=0; i<matrix.length; i++) {
if(binarySearch(matrix[i], target)) {
return true;
}
} return false;
} }
这么看来时间复杂度必须在线性的基础上才行。观察一下矩阵不难发现把矩阵逆时针旋转45度类似一棵二叉查找树。所以就可以模仿二叉查找树的方法来做了。
这样的话时间复杂度就是O(m + n);AC。
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length==0 || matrix[0].length==0) {
return false;
} int i = 0, j = matrix[0].length - 1; while(i < matrix.length && j >= 0) {
int cur = matrix[i][j];
if(cur == target) {
return true;
}
else if(cur < target) {
i ++;
}
else {
j --;
} }
return false; } }
LeetCode——Search a 2D Matrix II的更多相关文章
- [LeetCode] 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 Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- 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
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- 【刷题-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 ...
随机推荐
- Postgres数据库备份与还原命令
备份 pg_dump.exe -c -b -E UTF8 -U postgres -h 127.0.0.1 -p 5432 -f "f:\testdb001.bak" testdb ...
- 小任务tasklet应用
一个使用tasklet的中断程序首先会通过执行中断处理程序来快速完成上半部分的工作,接着通过调度tasklet使得下半部分的工作得以完成,但是下半部分何时执行属于内核的工作. 由于tasklet依靠软 ...
- zip文件内存中解压读取
// 构造zip输入流 ZipInputStream zip = new ZipInputStream(fis,Charset.forName("gbk")); byte[] tm ...
- Windoows窗口程序六
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...
- linux -- ubuntuserver 安装Apache后,修改默认目录和分布式配置文件可执行
装了apache后,访问出现The requested URL *** was not found on this server,查看了下/etc/httpd/conf/httpd.conf,发现原因 ...
- MVC演化 (转至http://www.cnblogs.com/wubaiqing/archive/2011/10/26/2225438.html)
MVC已经成为我们最常误用的模式,人们之所以常常误用MVC,很大程度上是因为混淆了不同的MVC变体. Classic MVC Classic MVC 大概上世纪七十年代,Xerox PARC的Tryg ...
- 基于Linux的智能家居的设计(4)
3 开发环境的搭建 本次课题使用的开发环境比較特殊.没有一个现成的集成开发环境,需要自己一步一步的搭建开发环境,开发环境的搭建的过程十分复杂,并且假设没有这个开发环境本次课题就无法进行. 因此.在进 ...
- hadoop学习笔记之-hbase完全分布模式安装-5
http://blog.csdn.net/lichangzai/article/details/8441975 http://blog.csdn.net/jpiverson/article/detai ...
- TFS 强制删除锁定文件(数据库)
TFS:TFS2010 VS:VS2012 OS:Windows2008 DB:Sqlserver2008 R2 我们在团队开发当中,版本控制是一个不可忽略的工具.我们团队使用的是TFS2010这个版 ...
- mysql中参数--init-file的作用是什么呢?
需求描述: 今天在修改测试环境mysql数据库中root用户密码的时候,用到了--init-file参数, 所以,就在这里说下该参数的作用. 概念解释: 参数:--init-file=file_nam ...