问题描述:
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int tmp;
int row = matrix.size();
int col = matrix[0].size();
//沿主对角线翻转
for(int i=0; i<row-1; i++){
for(int j=i+1; j<col; j++){
tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
//再左右翻转
for(int j=0; j<col/2; j++){
for(int i=0; i<row; i++){
tmp = matrix[i][j];
matrix[i][j] = matrix[i][col-j-1];
matrix[i][col-j-1] = tmp;
}
}
}
};