leetcode - 48.Rotate Image

leetcode - 48.Rotate Image_第1张图片

python式解法

利用zip函数和*,首先将matrix反向,再利用zip压缩即可,*表示选择列表中的子列表,*list

class Solution:
    def rotate(self, matrix):
        """
        Do not return anything, modify matrix in-place instead.
        """
        matrix[::] = zip(*matrix[::-1])
索引法
class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        n = len(matrix[0])
        matrix[::] = [[matrix[n-j-1][i] for j in range(n)] for i in range(n)]

你可能感兴趣的:(leetcode)