javascript 旋转方形矩阵90度
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 旋转方形矩阵90度相关的知识,希望对你有一定的参考价值。
// Rotate a matrix 90 degrees clockwise
// squares matrix
// [[1,2,3],
// [4,5,6],
// [7,8,9]]
// The result should be:
// [[7,4,1],
// [8,5,2],
// [9,6,3]]
function rotate(mat){
const newMatrix = [];
//this will create the number of array the matrix needs.
for (let i=0; i < mat.length; i++) {
newMatrix.push([]);
}
for (let i=0; i < mat.length; i++) {
for (let j=0; j < mat.length; j++) {
newMatrix[j][mat.length -1 -i] = mat[i][j];
console.log(j, i, mat.length -1 -i, mat[i][j]);
}
}
return newMatrix;
}
rotate(
[[1,2,3],
[4,5,6],
[7,8,9]]
)
以上是关于javascript 旋转方形矩阵90度的主要内容,如果未能解决你的问题,请参考以下文章