今天碰到这个问题,后来发现这个问题的最优解比较有意思,如下:
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?
解题思路1:先将矩阵转置,然后将矩阵的每一行翻转,就可以得到所要求的矩阵了。为便于理解,制作了如下示意图.
Figure 1: Illustration of transpose operation. Only the elements highlighted by green need be moved around. Do not touch yellow ones.
class Solution: # @param matrix, a list of lists of integers