python旋转矩阵

顺时针旋转,先将矩阵倒序,再转置

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        matrix[::] = [[row[i] for row in matrix[::-1]] for i in range(len(matrix[0]))]
class Solution:
      def rotate(self, matrix):
            matrix[:] = map(list,zip(*matrix[::-1]))

逆时针旋转,先将矩阵转置,再倒序

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        matrix[::] = [[row[i] for row in matrix] for i in range(len(matrix[0]))][::-1]

你可能感兴趣的:(python,算法)