题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
http://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
先进行上下翻转,再进行斜对角线翻转,可实现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;
}
};
查看3道真题和解析