• [leetcode] 240. Search a 2D Matrix II


    题目大意

    https://leetcode.com/problems/search-a-2d-matrix-ii/

    240. Search a 2D Matrix II

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

    编写一个高效的算法,从一个m × n矩阵中寻找一个值。矩阵具有如下性质:

    每一行的整数从左向右递增
    每一列的整数从上往下递增

    测试样例见题目描述。

    解题思路

    解法一:从矩阵的右上角(屏幕坐标系)开始,执行两重循环;外循环递增枚举每行,内循环递减枚举列

    class Solution(object):
        def searchMatrix(self, matrix, target):  # 48ms
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
            if not len(matrix) or not len(matrix[0]):
                return False
            m, n = len(matrix), len(matrix[0])
            r, c = 0, n - 1
            while r < m and c >= 0:
                if matrix[r][c] == target:
                    return True
                elif matrix[r][c] > target:
                    c -= 1
                else:
                    r += 1
            return False

    算法复杂度:O(m + n)

    类似思路的实现:

    class Solution(object):        
        def searchMatrix(self, matrix, target):  # 48ms
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
            if not len(matrix) or not len(matrix[0]):
                return False
            y = len(matrix[0]) - 1
            for x in range(len(matrix)):
                while y and matrix[x][y] > target:
                    y -= 1
                if matrix[x][y] == target:
                    return True
            return False

    解法二:循环枚举行,二分查找列

    class Solution(object):       
        def searchMatrix(self, matrix, target):  # 二分查找 148ms
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
            if not len(matrix) or not len(matrix[0]):
                return False
            y = len(matrix[0]) - 1
            
            def binary_search(nums, low, high):
                while low <= high:
                    mid = (low + high) / 2
                    if nums[mid] > target:
                        high = mid - 1
                    else:
                        low = mid + 1
                return high

    算法复杂度:O(m * logn)

    参考:

    http://bookshadow.com/weblog/2015/07/23/leetcode-search-2d-matrix-ii/

    https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/183609/An-intelligible-Python-solution-beats-99.64-48ms

  • 相关阅读:
    python基础11——seek应用&文件修改
    emmm......就当练习了系列08
    python基础10——文件操作x/b模式&控制指针移动
    emmm......就当练习了系列07
    emmm......就当练习了系列06
    python基础09——文件操作
    已经free的pycharm突然"抢钱"?粗暴的搞定它!
    由于系统掉电,导致磁盘扩容时,系统进入救援模式
    关于/etc/fstab的理解
    Linux磁盘
  • 原文地址:https://www.cnblogs.com/bymo/p/9897145.html
Copyright © 2020-2023  润新知