Skip to content

Instantly share code, notes, and snippets.

@robbdimitrov
Last active April 10, 2022 11:01
Show Gist options
  • Save robbdimitrov/335ebc42d7f794ed8214711ceff1e98e to your computer and use it in GitHub Desktop.
Save robbdimitrov/335ebc42d7f794ed8214711ceff1e98e to your computer and use it in GitHub Desktop.
Image rotation
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function(matrix) {
const n = matrix.length;
for (let i = 0; i < Math.floor(n / 2); i++) {
for (let j = i; j < n - i - 1; j++) {
let positions = [
[j, n - 1 - i],
[n - 1 - i, n - 1 - j],
[n - 1 - j, i],
[i, j]
];
let value = matrix[positions[3][0]][positions[3][1]];
for (let pos of positions) {
const temp = matrix[pos[0]][pos[1]];
matrix[pos[0]][pos[1]] = value;
value = temp;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment