Skip to content

Instantly share code, notes, and snippets.

@optimistiks
Created November 1, 2023 15:25
Show Gist options
  • Select an option

  • Save optimistiks/ae426848bdbe111217c13428fd8be9ef to your computer and use it in GitHub Desktop.

Select an option

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.
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