Lintcode 161. Rotate Image (Medium) (Python)

Rotate Image

Description:

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).

Example
Given a matrix

[
[1,2],
[3,4]
]
rotate it by 90 degrees (clockwise), return

[
[3,1],
[4,2]
]
Challenge
Do it in-place.

Code:

class Solution:
    """
    @param matrix: a lists of integers
    @return: nothing
    """
    def rotate(self, matrix):
        # write your code here
        for i in range(len(matrix)):
            for j in range(i, len(matrix[0])):
                matrix[i][j] , matrix[j][i] = matrix[j][i], matrix[i][j]
        for i in range(len(matrix)):
            matrix[i].reverse()
        return matrix

猜你喜欢

转载自blog.csdn.net/weixin_41677877/article/details/82343521