题目
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?
顺时针旋转90,可以看做是沿副对角线翻折一次,然后水平翻折一次
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n=matrix.size()-1;
for(int i=0;i<=n;i++){
for(int j=0;j<n+1-i;j++){
swap(matrix[i][j],matrix[n-j][n-i]);
}
}
for(int i=0;i<=n/2;i++){
for(int j=0;j<=n;j++){
swap(matrix[i][j],matrix[n-i][j]);
}
}
}
};