# 048 rotate image

### 48. Rotate Image

Medium

• 上下翻转规律 [i][:] --> [n-1-i][:]
• 对角线变换的规律是 [i][j] --> [j][i]

1 1 1    3 3 3    3 2 1
2 2 2 -> 2 2 2 -> 3 2 1
3 3 3    1 1 1    3 2 1

class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
# 上下翻转
for i in range(n/2):
matrix[i], matrix[n-1-i] = matrix[n-1-i], matrix[i]
# 主对角线翻转
for i in range(n):
for j in range(i+1,n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]


http://www.lifeincode.net/programming/leetcode-rotate-image-java/


1   2   3   4   5

6   7   8   9   10

11  12  13  14  15

16  17  18  19  20

21  22  23  24  25



class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
for i in range(n/2):
for j in range(n-n/2):
matrix[i][j], matrix[~j][i], matrix[~i][~j], matrix[j][~i] = \
matrix[~j][i], matrix[~i][~j], matrix[j][~i], matrix[i][j]


class Solution:
def rotate(self, A):
A[:] = zip(*A[::-1])
# A[:] = map(list, zip(*A[::-1]))