Created
November 1, 2023 15:25
-
-
Save optimistiks/ae426848bdbe111217c13428fd8be9ef to your computer and use it in GitHub Desktop.
Given an n×n matrix, rotate the matrix 90 degrees clockwise. The performed rotation should be in place, i.e., the given matrix is modified directly without allocating another matrix.
This file contains hidden or 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 rotateImage(matrix) { | |
| let l = 0; | |
| let r = matrix.length - 1; | |
| while (l < r) { | |
| for (let i = 0; i < (r - l); ++i) { | |
| const t = l; | |
| const b = r; | |
| const topLeft = matrix[t][l + i]; | |
| matrix[t][l + i] = matrix[b - i][l]; | |
| matrix[b - i][l] = matrix[b][r - i]; | |
| matrix[b][r - i] = matrix[t + i][r]; | |
| matrix[t + i][r] = topLeft; | |
| } | |
| l += 1; | |
| r -= 1; | |
| } | |
| return matrix; | |
| } | |
| export { rotateImage }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment