题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
http://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param mat int整型二维数组
* @param n int整型
* @return int整型二维数组
*/
export function rotateMatrix(mat: number[][], n: number): number[][] {
const len: number = mat.length
// 矩阵对角转置
for (let i = 0; i < len; i++ ) {
for (let j = 0; j < i; j++ ) {
const temp = mat[i][j]
mat[i][j] = mat[j][i]
mat[j][i] = temp
}
}
// 每行翻转
for (let i = 0; i < len; i++ ) {
for (let j = 0; j < len / 2; j++ ) {
const temp = mat[i][j]
mat[i][j] = mat[i][len - j - 1]
mat[i][len - j - 1] = temp
}
}
return mat
}