Array
048. Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
即
对给定矩阵进行旋转,但是不能用额外的空间
Solutions
很简单的想法:
先转置,和互换
然后对每一行元素取反
class Solution:
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
for i in range(len(matrix)):
for j in range(i,len(matrix)):
matrix[i][j],matrix[j][i]=matrix[j][i],matrix[i][j]
matrix[i].reverse()
网友评论