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?
Subscribe to see which companies asked this question
分析:
找规律的题。
先沿着对角线(左上到右下)做对称变换,然后沿着竖直中心轴做对称变换。
ac代码:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int i,j,n=matrix.size(),k;
for(i=0;i<n;i++)
for(j=0;j<i;j++)
swap(matrix[i][j],matrix[j][i]);
for(i=0,j=n-1;i<j;i++,j--)
for(k=0;k<n;k++)
swap(matrix[k][i],matrix[k][j]);
}
};