问题:
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?
分析:
二维数组a[n][n]顺时针旋转90度,要解决这个问题,无疑,第一件事儿就是找规律。
当n=1时,不用动了。
当n=2时, 旋转之后变为
有:
a[0][0] = a[1][0]
a[1][0] = a[1][1]
a[1][1] = a[0][1]
a[0][1] = a[0][0]
在这里我们初步总结规律为:a[i][j] = a[n-1-j][i]
当n=3时,