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. vim 移植记录

    下载两个源码包: vim : ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2\ ncurses-5.8.tar.gz http://ftp.gnu.org ...

  2. 胖ap和瘦ap区别

    胖AP(FAT AP)模式:适合小面积无线覆盖,AP单独使用,无需TP-LINK无线控制器(AC)即可独立工作,无线组网成本低: 瘦AP(FIT AP)模式:适合大面积无线覆盖,通过TP-LINK无线 ...

  3. HBase shell 命令。

    HBase shell 命令. 进入hbase shell console$HBASE_HOME/bin/hbase shell如果有kerberos认证,需要事先使用相应的keytab进行一下认证( ...

  4. epel源报错怎么解决?

    http://mirrors.aliyun.com/centos/6/extras/x86_64/repodata/81fdefdd048c01dcc6cda1fd53aacec2a0613ea10d ...

  5. mybatis 一对多关系

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  6. Linux C定时器使用指南

    使用定时器的目的是为了周期性的执行某一任务,或者是到了某个指定时间去执行某一任务.要达到这一目的,一般有两个常见的方法.一个是用linux内部的三个定时器,另一个是用sleep, usleep函数让进 ...

  7. html块状元素、内联元素

    html块状元素.内联元素 原文在这 块级元素的分类 块级元素按照其应用于结构还是内容分为三种:结构化块状元素,终端块状元素,多目标块状元素. 一.结构化块状元素 这类元素用于构造文档的结构,一个好的 ...

  8. Android解决下拉刷新控件SwipeRefreshLayout和ViewPager的滑动冲突

    直接说明下我自己项目中的情况,如图: 外部嵌套任何一种refresh下拉控件之后,上方的viewpager左右滑动事件都受到影响,滑动不流畅,稍微有点向下的趋势就会触发刷新. 起初以为可能跟不同下拉控 ...

  9. 封装自己的yQuery

    function myAddEvent(obj, sEv, fn) { if (obj.attachEvent) { obj.attachEvent('on' + sEv, fn) } else { ...

  10. 用route命令解决多出口的问题

    网络已经走进了我们的生活.工作.学习之中,大多数单位.公司都已经连接到了Internet.但是,因为各种原因,有这样一个问题存在.就是:这些单位即有到公网(Internet)的出口连接,也有到专网(单 ...