Rotate Image
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?
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int i,j,n = matrix.size();
for(i = 0; i < n; i++) {
reverse(matrix[i].begin(), matrix[i].end());
}
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
swap(matrix[i][j],matrix[n-1-j][n-1-i]);
}
}
}
};
思路:解法空间复杂度O(1)。先每行倒序,然后按副对角线对称。