LeetCode11 旋转图像

LeetCode11 旋转图像_第1张图片

LeetCode11 旋转图像_第2张图片

class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """
         #先转置再左右对称翻转
        if not matrix or not matrix[0]:#判断
            return matrix
        n = len(matrix)
        
        for i in range(n):#转置
            for j in range(i + 1, n):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
                
        for row in matrix:
            for i in range(n // 2):#左右对翻
                row[i], row[n - 1 - i] = row[n - 1 - i], row[i]
                
        return matrix

参考链接

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