Skip to content

Instantly share code, notes, and snippets.

@wataruoguchi
Created August 2, 2019 18:50
Show Gist options
  • Save wataruoguchi/8d3c8aac107dcc43f4f41d2691fa58be to your computer and use it in GitHub Desktop.
Save wataruoguchi/8d3c8aac107dcc43f4f41d2691fa58be to your computer and use it in GitHub Desktop.
// https://www.geeksforgeeks.org/inplace-rotate-square-matrix-by-90-degrees/
// https://practice.geeksforgeeks.org/problems/rotate-by-90-degree/0
function rotateMatrixAntiClockwise(matrix) {
let x;
let y;
let length = matrix.length;
// Consider all squares.
// If the matrix is 3x3, you need to rotate one square.
// If it's 4x4, or 5x5, it's gonna be two.
// If it's 6x6, or 7x7, it's gonna be three.
// "x" represents number of squares
for (x = 0; x < length / 2; x++) {
// "y" represents which square you're manipurating.
// The range is x < y < length - x - 1
// With 4x4 matrix, it'll be:
// 0 < y < 4 - 0 - 1 ... The first (outside) square
// 1 < y < 4 - 1 - 1 ... The second (inside) square
for (y = x; y < length - x - 1; y++) {
// 4x4 x=0 tmp=[0][0]1
// 4x4 x=1 tmp=[1][1]6
let tmp = matrix[x][y];
// 4x4 x=0 [0][0] = [0][3]4
// 4x4 x=1 [1][1] = [1][2]7
matrix[x][y] = matrix[y][length-1-x];
// 4x4 x=0 [0][3] = [3][3]16
// 4x4 x=1 [1][2] = [2][2]11
matrix[y][length-1-x] = matrix[length-1-x][length-1-y];
// 4x4 x=0 [3][3] = [3][0]13
// 4x4 x=1 [2][2] = [2][1]10
matrix[length-1-x][length-1-y] = matrix[length-1-y][x];
// 4x4 x=0 [3][0] = tmp
// 4x4 x=1 [2][1] = tmp
matrix[length-1-y][x] = tmp;
}
}
return matrix;
}
function rotateMatrixClockwise(matrix) {
let x;
let y;
let length = matrix.length;
for (x = 0; x < length / 2; x++) {
for (y = x; y < length - x - 1; y++) {
// 4x4 x=0 y=0 tmp = [0][0]1
// 4x4 x=0 y=1 tmp = [0][1]2
// 4x4 x=0 y=2 tmp = [0][2]3
// 4x4 x=1 y=1 tmp = [1][1]6
let tmp = matrix[x][y];
// 4x4 x=0 y=0 [0][0] = [3][0]13
// 4x4 x=0 y=1 [0][1] = [2][0]9
// 4x4 x=0 y=2 [0][1] = [1][0]5
// 4x4 x=1 [1][1] = [2][1]10
matrix[x][y] = matrix[length - 1 - y][x];
// 4x4 x=0 y=0 [3][0] = [3][3]16
// 4x4 x=0 y=1 [2][0] = [3][2]15
// 4x4 x=0 y=2 [1][0] = [3][1]14
// 4x4 x=1 [2][1] = [2][2]11
matrix[length - 1 - y][x] = matrix[length - 1 - x][length - 1 - y];
// 4x4 x=0 y=0 [3][3] = [0][3]4
// 4x4 x=0 y=1 [3][2] = [1][3]8
// 4x4 x=0 y=2 [3][1] = [2][3]12
// 4x4 x=1 [2][2] = [2][1]7
matrix[length - 1 - x][length - 1 - y] = matrix[y][length - 1 - x];
// 4x4 x=0 [0][3] = tmp
// 4x4 x=0 [1][2] = tmp
matrix[y][length - 1 - x] = tmp;
}
}
return matrix;
}
function printMatrix(matrix) {
let idxX;
let idxY;
let length = matrix.length;
let printArr = [];
for (idxY = 0; idxY < length; idxY++) {
for (idxX = 0; idxX < length; idxX++) {
printArr.push(matrix[idxY][idxX]);
}
console.log(printArr.join(' | '));
printArr = [];
}
console.log(matrix.map((row) => '-').join(' - '));
}
const matrixEx1 = [[1,2,3],[4,5,6],[7,8,9]];
// printMatrix(matrixEx1);
// printMatrix(rotateMatrixAntiClockwise(matrixEx1));
let matrix4x4 = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];
printMatrix(matrix4x4);
rotateMatrixAntiClockwise(matrix4x4);
printMatrix(matrix4x4);
rotateMatrixClockwise(matrix4x4);
printMatrix(matrix4x4);
const matrix6x6 = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],
[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]];
printMatrix(matrix6x6);
printMatrix(rotateMatrixAntiClockwise(matrix6x6));
printMatrix(rotateMatrixClockwise(matrix6x6));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment