【问题标题】:Rotate a matrix by 90 degrees将矩阵旋转 90 度
【发布时间】:2020-09-04 23:21:40
【问题描述】:

背景

我正在解决一个标准的LeetCode Question 48。我知道有很多解决方案,但我正在尝试用一种对我有意义的方法来解决这个问题。我基本上是在尝试将矩阵旋转 90 度。我正在尝试交换方阵每一层的每个值。

这是我的代码:

def rotate_image(matrix):
    top = 0
    bottom = len(matrix)
    left = 0
    right = len(matrix[0])
    total_layers = round(bottom / 2)

    for i in range(0, total_layers):

        for j in range(left, right - 1):
            top_left = matrix[top][j]
            top_right = matrix[j][right - 1]
            bottom_right = matrix[bottom - 1][right - (j + 1)]
            bottom_left = matrix[bottom - (1+j)][left]
            matrix[top][j] = bottom_left
            matrix[j][right - 1] = top_left
            matrix[bottom - 1][right - (j + 1)] = top_right
            matrix[bottom - (1 + j)][left] = bottom_right
        top += 1
        left += 1
        right -= 1
        bottom -= 1
    print(matrix)

由于某种原因,我的代码通过了大多数情况,但是当我尝试以下情况时

print(rotate_image([[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]]))

它失败了。我花了 2 个小时尝试调试它,我可能正在查看某些东西,但不确定它到底是什么。

我希望得到一些反馈。

【问题讨论】:

  • 用笔和纸对一个小的失败示例执行您的算法。如果部分结果确实符合您对最终结果的期望,请在每一步之后进行比较。

标签: python algorithm multidimensional-array data-structures


【解决方案1】:

我可以为您提供另一种干净的方法来将方阵旋转 90 度。

第 1 步:跨对角线交换元素。 第 2 步:水平镜像元素

你得到了你的旋转矩阵。

您还可以根据需要旋转矩阵的方向来调整水平和垂直镜像。

例如

你得到了 90° 旋转矩阵

【讨论】:

    【解决方案2】:

    您遇到了索引问题。让我们检查您的部分代码:

    for j in range(left, right - 1):
        top_left = matrix[top][j]
        top_right = matrix[j][right - 1]
        bottom_right = matrix[bottom - 1][right - (j + 1)]
    

    考虑当j 取值right-2 时会发生什么。您将从right - (j+1) = right - (right-2+1)=1 列中提取一个条目。也就是说,无论您在哪一层,您都将从1-th 列中提取。需要调整回图层的起点,即不是right - (j+1)-th 列,需要访问left + right - (j+1)-th 列。此外,请注意left + right 实际上是一个常数,它等于矩阵的长度。您可以利用 Python 中的负索引并改为访问 -(j+1)-th 列。

    另外,我们已经被告知它是一个方阵,所以topleft 总是相同的,bottomright 总是相等的。

    这是执行我上面提到的更正的代码:

    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        top = 0
        bottom = len(matrix)
        total_layers = round(bottom / 2)
    
        for i in range(0, total_layers):
            for j in range(top, bottom - 1):
                top_left = matrix[top][j]
                top_right = matrix[j][bottom - 1]
                bottom_right = matrix[bottom - 1][ - (j + 1)]
                bottom_left = matrix[- (1+j)][top]
                matrix[top][j] = bottom_left
                matrix[j][bottom-1] = top_left 
                matrix[bottom - 1][- (j + 1)] = top_right
                matrix[- (1 + j)][top] = bottom_right
            top += 1
            bottom -= 1
    

    【讨论】:

    • 这是迄今为止唯一试图帮助 OP 解决有关其代码的问题的答案。
    【解决方案3】:

    下一种方法选择矩阵的左上四分之一,存储该四分之一的元素并以循环方式重新排列其他四分之一的相应元素

    def rotate_image(matrix):
        n = len(matrix)
        for i in range(0, (n + 1) // 2):
            for j in range(0, n // 2):
                storage = matrix[i][j]
                matrix[i][j] = matrix[n - j - 1][i]
                matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
                matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
                matrix[j][n - i - 1] = storage
        for i in range(len(matrix)):
            print(matrix[i])
    
    mt = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
    
    rotate_image(mt)
    print()
    
    mt = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21,22, 23,24,25]]
    
    rotate_image(mt)
    
    [13, 9, 5, 1]
    [14, 10, 6, 2]
    [15, 11, 7, 3]
    [16, 12, 8, 4]
    
    [21, 16, 11, 6, 1]
    [22, 17, 12, 7, 2]
    [23, 18, 13, 8, 3]
    [24, 19, 14, 9, 4]
    [25, 20, 15, 10, 5]
    

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多