leetcode240——搜索二维矩阵(medium)

1、题目描述

编写一个高效的算法来搜索 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

2、题解

方法一:暴力搜索法

方法一最容易想到,直接使用两个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)\)

方法三:利用本题矩阵的特色

既然题目告诉咱们矩阵每行的元素从左到右升序排列,每列的元素从上到下升序排列,那么咱们能够利用这一特性来巧妙解题。

  • 首先设置变量row表示行标,col表示列标,将row的初始值设为0,表示第一行,将col的初始值设为矩阵最后一列的下标;
  • 而后使用一个while循环遍历矩阵,若matrix[row][col] > target成立时,说明当前值比目标值target大,列标col须要左移来找到更小的值与target相比较;若matrix[row][col] < target成立时,说明当前值比目标值target小,行标row须要下移来找到更大的值与target相比较;若matrix[row][col] == target成立时,说明找到了目标值target,直接返回True便可;
  • 最后,若遍历结束仍然没有找到目标值target,说明矩阵中不存在目标值taregt,返回False便可。

注意:

  • 与目标值比较的初始值选取的位置必须在矩阵的左下角和右上角处

完成时间: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)\)

相关文章
相关标签/搜索