Skip to content

Instantly share code, notes, and snippets.

@pitsanujiw
Last active January 11, 2021 09:18
Show Gist options
  • Save pitsanujiw/b30222a9a0bc6d2b3cec6df5ff5f42ba to your computer and use it in GitHub Desktop.
Save pitsanujiw/b30222a9a0bc6d2b3cec6df5ff5f42ba to your computer and use it in GitHub Desktop.
Rotate Matrix (Transpose)
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