卖萌的弱渣

I am stupid, I am hungry.

Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up: Could you do this in-place?

Solution

(Rotate-Image.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        ret = []
        if matrix == None:
            return ret

        n = len(matrix)
        for i in range(n):
            for j in range(n-i-1):
                matrix[i][j],matrix[n-j-1][n-i-1] = matrix[n-j-1][n-i-1],matrix[i][j]
        matrix.reverse()