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的更多相关文章

  1. [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 ...

  2. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  3. LeetCode Search a 2D Matrix II (技巧)

    题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...

  4. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  5. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  6. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  7. 【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 ...

  8. 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 ...

  9. 【刷题-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 ...

随机推荐

  1. ubuntu下man帮助文档不全怎么办?如何解决?

    真心后悔用ubuntu学习linux 发现很多东西不全,kate没有,vi版本低,帮助文档不全一系列的问题.ubuntu下man帮助文档不全怎么办?如何解决?不用担心,下边小编就为大家带来最详细的解决 ...

  2. hadoop中的Jobhistory历史服务器

    1.  启动脚本 mr-jobhistory-daemon.sh start historyserver 2. 配置说明 jobhistory用于查询每个job运行完以后的历史日志信息,是作为一台单独 ...

  3. java多线程面试题整理及回答

    1)现在有T1.T2.T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行? 这个线程问题通常会在第一轮或电话面试阶段被问到,目的是检测你对”join”方法是否熟悉.这个多线程问题比 ...

  4. CDH离线安装

    1. 安装准备 系统:Centos 6 Cloudera Manager分配如下: 安装版本:CDH-5.8.0 所需安装文件 CDH相关 CDH-5.8.0-1.cdh5.8.0.p0.42-el6 ...

  5. JavaScript 关键字快速匹配

    来源: http://www.cnblogs.com/index-html/archive/2013/04/17/js_keyword_match.html http://www.etherdream ...

  6. Python 变量类型和运算符

    -*- coding:utf-8 -*- ''' if语法 if conditon: [tab键] command [tab键] command ... else: [tab键] command [t ...

  7. 转:Python操作SQLServer示例

    注:此文也是转载,2018年1月发现此文阅读量过万,略感不安.当时只是为了自己存档学习,未粘此文的原始连接.如有侵权,通过即删除,敬请谅解! 从网上找的,估计原文是:Python操作SQLServer ...

  8. Mysql经常使用基本命令汇总及默认账户权限与改动

    一直仅仅是在浅显利用数据库存储数据.也被windows惯坏了.非常多命令使用的时候记不起来.so,换LINUX系统!不再使用GUI管理数据库!也想深入学习下Mysql.从权限管理開始.也就诞生了这篇学 ...

  9. SVN版本冲突,导致出现Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。

    SVN版本冲突,导致出现Files 的值“ < < < < < < < .mine”无效.路径中具有非法字符. 右键编辑.... 打开并删除含有'<&l ...

  10. 微软ASP.NET网站部署指南(2):部署SQL Server Compact数据库

    1. 综述 对于数据库訪问,Contoso University程序要求以下的软件必须随程序一起部署.由于不属于.NET Framework: SQL Server Compact (数据库引擎) A ...