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) {
if(matrix.empty()||matrix[0].empty()||
matrix.size()==0||matrix[0].size()==0){
return;
}
int m=matrix.size(),n=matrix[0].size(),tmp;
//top <---> bottom
for(int i=0;i<m>>1;i++){
for(int j=0;j<n;j++){
tmp=matrix[i][j];
matrix[i][j]=matrix[n-1-i][j];
matrix[n-1-i][j]=tmp;
}
}
//top-right <--> bottom-left
for(int i=0;i<m;i++){
for(int j=i+1;j<n;j++){
tmp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=tmp;
}
}
}
};