문제
You are given an
n x n
2D matrix
representing an image, rotate the image by 90 degrees (clockwise).You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Constraints:
n == matrix.length == matrix[i].length
1 <= n <= 20
1000 <= matrix[i][j] <= 1000
풀이
재영
/** * @param {number[][]} matrix * @return {void} Do not return anything, modify matrix in-place instead. */ var rotate = function(matrix) { const copiedMatrix = JSON.parse(JSON.stringify(matrix)); const matrixLength = matrix.length; for (let i = 0; i < matrixLength; i += 1) { for (let j = 0; j < matrixLength; j += 1) { const row = matrixLength - 1 - j; const col = i; matrix[i][j] = copiedMatrix[row][col]; }; }; };
은찬
var rotate = function(matrix) { const tmp = []; const length = matrix.length; for(let i = 0; i < length; i++){ tmp.push([...matrix[i]]); } for(let i = 0; i < length; i++){ for(let j = 0; j < length; j++){ matrix[i][j] = tmp[length - j - 1][i]; } } };
효성
var rotate = function(matrix) { const len = matrix.length; const copyMatrix = Array.from(Array(len), () => Array(len)); for(let i = 0; i < len; i++) { for(let j = 0; j < len; j++) { copyMatrix[i][j] = matrix[i][j]; } } for(let i = 0; i < len; i++) { for(let j = 0; j < len; j++) { matrix[j][len - 1 - i] = copyMatrix[i][j]; } } };