Last active
April 10, 2022 11:01
-
-
Save robbdimitrov/335ebc42d7f794ed8214711ceff1e98e to your computer and use it in GitHub Desktop.
Image rotation
This file contains 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
/** | |
* @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