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 n = matrix.size();
for (int i = 0; i < n;i++)
for (int j = i; j < n; j++){
swap(matrix[i][j], matrix[j][i]);
}
for (int i = 0; i < n; i++)
for (int beg = 0,end=n-1; beg< end; beg++,end--){
swap(matrix[i][beg], matrix[i][end]);
}
}
};