先进行上下翻转,再进行斜对角线翻转,可实现90度旋转。
class Solution {
public:
vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) {
// write code here
int temp;
for(int i=0; i<n/2; i++){
for(int j=0; j<n; j++){
temp = mat[i][j];
mat[i][j] = mat[n-i-1][j];
mat[n-i-1][j] = temp;
}
}
for(int i=0; i<n; i++){
for(int j=0; j<i; j++){
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
return mat;
}
};