题目:
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?
思路:
首先,要求原地调整,那么需要寻找变换规律。举个例子:
对于3x3的矩阵,如顺时针调整(0,0)->(0,2)->(2,2)->(2,0)
对于4x4的矩阵,如顺时针调整(0,0)->(0,3)->(3,3)->(3,0);(0,1)->(1,3)->(3,2)->(2,0);(0,2)->(2,3)->(3,1)->(1,0).
转换传递规律:对于一个nxn矩阵来说,(a,b)->(b,n-1 - a),并且该链式传递转换有......
阅读全文