首页 > 解决方案 > 为二维矩阵元素使用变量名以提高可读性

问题描述

在解决 Leetcode 问题时,我一直在努力使我的答案尽可能容易理解,这样我以后可以快速浏览它们并理解它们。为此,我将变量名称分配给二维列表中的感兴趣索引。当我反复看到“matrix[i][j+1]”及其变体时,我有时会忘记我正在处理的内容。

所以,对于这个问题:https ://leetcode.com/problems/maximal-square/

我写了这段代码:

class Solution:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
        
        maximum = 0
        
        for y in range(len(matrix)):
            for x in range(len(matrix[0])):
                
                #convert to integer from string
                matrix[y][x] = int(matrix[y][x])
                
                #use variable for readability
                current = matrix[y][x]
                
                #build largest square counts by checking neighbors above and to left
                #so, skip anything in first row or first column
                if y!=0 and x!=0 and current==1:
                    
                    #assign variables for readability. We're checking adjacent squares 
                    left = matrix[y][x-1]
                    up = matrix[y-1][x]
                    upleft = matrix[y-1][x-1]
                    
                    #have to use matrix directly to set new value
                    matrix[y][x] = current = 1 + min(left, up, upleft)
                    
                #reevaluate maximum
                if current > maximum:
                    maximum = current

        #return maximum squared, since we're looking for largest area of square, not largest side
        return maximum**2

我认为我以前没有见过人们这样做,我想知道这是否是一个坏主意,因为我有点维护一个值的两个版本。

抱歉,如果这是一个“编码风格”问题,因此只是一个意见问题,但我认为可能有一个我还没有找到的明确答案。

标签: pythonlistmatrix

解决方案


很难给出一个直截了当的答案,因为它可能因人而异。让我从您的查询开始:

当我反复看到“matrix[i][j+1]”及其变体时,我有时会忘记我正在处理的内容。

这取决于。具有中等编程知识的人不应该被二维矩阵的matrix[x-pos][y-pos]形状所迷惑。同样,如果您觉得不舒服,可以使用您在此处分享的方式。但是,您应该尝试同时采用和熟悉此类常见概念。

我认为我以前没有见过人们这样做,我想知道这是否是一个坏主意,因为我有点维护一个值的两个版本。

这根本不是一个坏主意。只要您考虑这样做是为了您的舒适,它就是“好的”。但是,如果您想与他人共享您的代码,那么使用太明显的东西可能不是一个好主意。它可能会降低您的代码对其他人的可理解性。但是,maintaining two versions of a value只要额外的内存是恒定的,您就不必担心 。

抱歉,如果这是一个“编码风格”问题,因此只是一个意见问题,但我认为可能有一个我还没有找到的明确答案。

你问这个问题绝对没问题。正如你所提到的,它确实是just a matter of opinion。您可以遵循一些标准语言指南,例如Google Python Style Guide。始终建议对此类coding style事物遵循一些标准。永远记住,一个good code总是self-documented和发表不必要的评论有时会让它变得无聊。还,

在这里,我分享了我的代码版本。如果您有任何问题,请随时发表评论。

# Time: O(m*n)
# Space: O(1)

class Solution:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
        """Given an m x n binary matrix filled with 0's and 1's,
        find the largest square containing only 1's and return its area.

        Args:
          matrix: An (m x n) string matrix.

        Returns:
          Area of the largest square containing only 1's.
        """
        maximum = 0
        
        for x in range(len(matrix)):
            for y in range(len(matrix[0])):
                # convert current matrix cell value from string to integer
                matrix[x][y] = int(matrix[x][y])
                
                # build largest square side by checking neighbors from up-row and left-column
                # so, skip the cells from the first-row and first-column
                if x != 0 and y != 0 and matrix[x][y] != 0:
                    # update current matrix cell w.r.t. the left, up and up-left cell values respectively
                    matrix[x][y] = 1 + min(matrix[x][y-1], matrix[x-1][y], matrix[x-1][y-1])
                    
                # re-evaluate maximum square side
                if matrix[x][y] > maximum:
                    maximum = matrix[x][y]

        # returning the area of the largest square
        return maximum**2

推荐阅读