Last active
January 11, 2021 09:18
-
-
Save pitsanujiw/b30222a9a0bc6d2b3cec6df5ff5f42ba to your computer and use it in GitHub Desktop.
Rotate Matrix (Transpose)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rotate(matrix: number[][]) { | |
let n = matrix.length; | |
// transpose matrix | |
for (let i = 0; i < n; i++) { | |
for (let j = i; j < n; j++) { | |
let tmp = matrix[j][i]; | |
matrix[j][i] = matrix[i][j]; | |
matrix[i][j] = tmp; | |
console.log(tmp,matrix[j][i]); | |
} | |
} | |
// reverse each row | |
for (let i = 0; i < n; i++) { | |
for (let j = 0; j < n / 2; j++) { | |
let tmp = matrix[i][j]; | |
matrix[i][j] = matrix[i][n - j - 1]; | |
matrix[i][n - j - 1] = tmp; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment