编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具备如下特性:python
示例:算法
现有矩阵 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] ]
给定 target = 5,返回 true。优化
给定 target = 20,返回 false。spa
来源:力扣(LeetCode)
连接:https://leetcode-cn.com/problems/search-a-2d-matrix-iicode
方法一最容易想到,直接使用两个for循环遍历矩阵,当遇到与target相等的值时直接返回True便可。此法显然不是出题人想要的结果。leetcode
完成时间:2020.05.07get
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j] == target: return True return False
方法一使用了两趟循环:io
时间复杂度:\(O(m * n)\) ,\(m\)指的是矩阵行数,\(n\)指的是矩阵列数。
空间复杂度:\(O(1)\)。for循环
方法二是对方法一的优化。因为矩阵的行和列都已经排好序,那么能够利用二分查找加快目标值的查找速度。具体作法是当按行遍历矩阵时,使用二分查找法对每行进行查找。
注意:
完成时间:2020.05.09
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ # 矩阵为空,直接返回False if not matrix: return False rows = len(matrix) columns = len(matrix[0]) for i in range(rows): left = 0 right = columns - 1 # 注意 while left <= right: # 注意要带等号,否则当数组只有一个值时,可能会漏掉结果 mid = (left + right) // 2 if matrix[i][mid] > target: right = mid - 1 elif matrix[i][mid] == target: return True elif matrix[i][mid] < target: left = mid + 1 return False
方法二使用了两趟循环:
时间复杂度:for循环的时间复杂度为\(O(m)\) ,\(m\)指的是矩阵行数,while循环的时间复杂度为 \(O(\log_{2}n)\),n为矩阵的列数,因此总的时间复杂度为\(O(m*\log_{2}n)\);
空间复杂度:\(O(1)\)。
既然题目告诉咱们矩阵每行的元素从左到右升序排列,每列的元素从上到下升序排列,那么咱们能够利用这一特性来巧妙解题。
注意:
完成时间:2020.05.07
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ if not matrix: return False row, col = 0, len(matrix[0]) - 1 while row < len(matrix) and col >= 0: if matrix[row][col] > target: col -= 1 elif matrix[row][col] < target: row += 1 else: return True return False
方法三使用了一趟循环:
时间复杂度:\(O(m + n)\) ,\(m\)指的是矩阵行数,\(n\)指的是矩阵列数。row的最大值不超过矩阵行数m,col的最大值不超过矩阵列数n。
空间复杂度:\(O(1)\)。