Thursday, December 24, 2015

[Leetcode] Rotate image

Problem

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?


Analysis

对于一个n*n的矩阵,可以像剥洋葱一样对每一层进行rotate,一共有n/2层

对于每一层,采用 matrix[i][j] = matrix[n-1-j][i] 进行 in-place的rotation

时间复杂度: O(n^2)
空间复杂度: O(n^2)


Code

No comments:

Post a Comment