题目

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.

题解:

虽然本题看似是矩阵问题,但是本着搜索题目关键字为第一步的原则,可以找到:each row are sorted,每一行按照顺序也是sorted。同时也是数组保存。

但是本题的难点就是如何将2D矩阵转换成1D,然后利用二分查找法来解决问题。转换的重点就在于每个点的位置,在矩阵表示中,我们习惯用(i,j)来表示一个点,所以这就有碍于我们使用low high mid来指向需要的位置。为了解决问题,第一步就是需要将这个矩阵按照顺序拉成一条线。

像题中的例子我可以将其转化为:

position: 0   1   2   3   4   5   6   7   8   9   10   11

values:   1   3   5   7   10 11 16 20  23 30  34  50

row:       0   0   0   0   1   1   1   1   2   2    2    2

column:        3   0         0      2    3

其中:行数rows=3,列数columns=4

如上,这个就是将2D矩阵转化成1行数组的对应表。所以对于二分查找法的初始值为:low=0,high=rows*columns-1(总共数值的个数,因为从0开始所以减1)。而为了能够方便在given 2D matrix找到需要比对的值,我们还是需要确定行数和列数,通过上表可以看出,行数是position/columns,而列数是position%columns, 这样一来,就能很容易的在原矩阵中定位到所需要的值。剩下其他的解题思路,就与二分查找法一模一样了。

时间复杂度O(log(rows*columns))

代码如下:

 1     public boolean searchMatrix(int[][] matrix, int target) {
 2         if(matrix.length==0||matrix[0].length==0||matrix==null)
 3             return false;
 4             
 5         int rows = matrix.length;
 6         int cols = matrix[0].length;
 7         
 8         int low = 0;
 9         int high = rows*cols-1;
         
         while(low<=high){
             int mid = (low+high)/2;
             int midValue = matrix[mid/cols][mid%cols];
             if(midValue == target)
                 return true;
             else if(midValue < target)
                 low = mid+1;
             else
                 high = mid-1;
         }
         return false;
     }

同时,也有另外一个解决该题的方法,就是利用两次二分查找法。因为所给矩阵第一列也是升序排列的,所以可以先对第一列进行二分查找,锁定该元素所在行数,然后再对列进行二分查找,即可判断target是否存在。这个的算法时间复杂度是O(log(rows)+log(columns))。

代码如下:

 1     public boolean searchMatrix(int[][] matrix, int target) {  
 2         if(matrix == null || matrix.length==0 || matrix[0].length==0)  
 3             return false;  
 4         int low = 0;  
 5         int high = matrix.length-1;  
 6         while(low<=high){  
 7             int mid = (low+high)/2;  
 8             if(matrix[mid][0] == target)
 9                 return true;  
             else if(matrix[mid][0] > target)  
                 high = mid-1; 
             else
                 low = mid+1;  
         }
         
         int row = high; //当从while中跳出时,low指向的值肯定比target大,而high指向的值肯定比target小
         
         if(row<0)  
             return false; 
             
         low = 0;  
         high = matrix[0].length-1;  
         while(low<=high){  
             int mid = (low+high)/2;  
             if(matrix[row][mid] == target)
                 return true;  
             else if(matrix[row][mid] > target)  
                 high = mid-1;
             else 
                 low = mid+1;  
         }     
         return false;  
     } 

Reference:

http://www.programcreek.com/2013/01/leetcode-search-a-2d-matrix-java/

http://blog.csdn.net/linhuanmars/article/details/24216235

Search a 2D Matrix leetcode java的更多相关文章

  1. Search a 2D Matrix ——LeetCode

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  2. Search a 2D Matrix leetcode

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. LeetCode Search a 2D Matrix II

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

  4. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

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

  7. [LeetCode] Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  9. LeetCode: Search a 2D Matrix 解题报告

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

随机推荐

  1. 【知了堂学习笔记】java 接口与抽象类

    本次主角:抽象类 .接口. 对于皮皮潇这样一类的Java初学者来说,接口和抽象类如果不去花大量的精力与时间是很难弄清楚的,而我也是在最近这周的项目学习中感觉到了我对这两个概念不熟悉,所以导致对一些问题 ...

  2. python爬虫+词云图,爬取网易云音乐评论

    又到了清明时节,用python爬取了网易云音乐<清明雨上>的评论,统计词频和绘制词云图,记录过程中遇到一些问题 爬取网易云音乐的评论 一开始是按照常规思路,分析网页ajax的传参情况.看到 ...

  3. 深度学习基础系列(七)| Batch Normalization

    Batch Normalization(批量标准化,简称BN)是近些年来深度学习优化中一个重要的手段.BN能带来如下优点: 加速训练过程: 可以使用较大的学习率: 允许在深层网络中使用sigmoid这 ...

  4. HTTP请求的GET与POST方式的区别

    Form中的get和post方法,在数据传输过程中分别对应了HTTP协议中的GET和POST方法.二者主要区别如下: 1)Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据: 2)Ge ...

  5. 学会使用DNSPod,仅需三步

    学会使用DNSPod,仅需三步   第一步:在DNSPod添加记录 1.访问 https://www.dnspod.cn网站,在DNSPod官网首页的右上角,有[注册],如下图所示,点击[注册]按钮 ...

  6. python中的urlencode和urldecode

    python将字符串转化成urlencode ,或者将url编码字符串decode的方法: 方法1: urlencode:urllib中的quote方法 >>> from urlli ...

  7. zip函数实际应用

    一个需求,把两个列表要同时用for循环贴到模板上,用zip在后端组成  元祖组成的列表  这样在模板就可以用.0   .1的方式,类似python中 x[0]   x[1] 显示出来. html显示: ...

  8. 【状压dp】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem E. Guessing Game

    题意:给你n个两两不同的零一串,Alice在其中选定一个,Bob去猜,每次询问某一位是0 or 1.问你最坏情况下最少要猜几次. f(22...2)表示当前状态的最小步数,2表示这位没确定,1表示确定 ...

  9. 【漏洞预警】方程式又一波大规模 0day 攻击泄漏,微软这次要血崩

    一大早起床是不是觉得阳光明媚岁月静好?然而网络空间刚刚诞生了一波核弹级爆炸!Shadow Brokers再次泄露出一份震惊世界的机密文档,其中包含了多个精美的 Windows 远程漏洞利用工具,可以覆 ...

  10. 安卓中WebKit的使用

    1.在安卓开发中,使用webkit显示网页 步骤: ①初始化一个webkit控件: ②获取webkit的WebSettings对象: ③设置javascript为enable ④为webkit设置&q ...