Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeserk/c6efdf67b42ae3ff5c574202cf252917 to your computer and use it in GitHub Desktop.
Save codeserk/c6efdf67b42ae3ff5c574202cf252917 to your computer and use it in GitHub Desktop.
/**
* Rotates the incoming matrix 90deg
* @param matrix
*/
export function rotate90deg(matrix: unknown[][]): void {
const n = matrix.length === 0 ? 0 : matrix[0].length
for (let ring = 0; ring < Math.floor(n / 2); ring++) {
for (let position = ring; position < n - ring - 1; position++) {
const backupRight = matrix[position][n - ring - 1]
// Top -> Right
matrix[position][n - ring - 1] = matrix[ring][position]
// Left -> Top
matrix[ring][position] = matrix[n - position - 1][ring]
// Bottom -> Left
matrix[n - position - 1][ring] = matrix[n - ring - 1][n - position - 1]
// Right -> Bottom
matrix[n - ring - 1][n - position - 1] = backupRight
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment