Leetcode_Python 48 旋转图像

Leetcode_Python 48 旋转图像_第1张图片

解题思路

本题思路就是,先将二维数组左右对调,再沿着反对角线对调。

代码

class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """
        N = len(matrix)     #列数==列数
        for i in range(N):
            for j in range(N//2):
                a = matrix[i][N-1-j]
                matrix[i][N-1-j] = matrix[i][j]
                matrix[i][j] = a
        for i in range(N):
            for j in range(N-i-1):
                a = matrix[N-j-1][N-i-1]
                matrix[N-j-1][N-i-1] = matrix[i][j]
                matrix[i][j] = a
        return matrix
            

你可能感兴趣的:(LeetCode,leetcode,数组_二维数组变换)