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?
Show Company Tags
Show Tags
解法1:特殊解法
如果是out-of-place的解法,就用temp[j][n - 1 - j] = matrix[i][j]
如果要inplace的解法:可以有这么几种:
- 先按逆对角线翻转一次,然后按x轴中线翻转一次。
- 或者呢也可以先transpose,然后把每一行翻转。
个人感觉第二种解法比较好记也比较好写。不容易出错
Java