题目:
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.size() <= 1) return;
int n = matrix.size();
for(int i = 0; i < n/2; ++i){//上下翻转
vector<int> tmp = matrix[n-1-i];
matrix[n-1-i] = matrix[i];
matrix[i] = tmp;
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < i; ++j){//以对角线为轴翻转
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
}
};