-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0048-rotate-image.js
More file actions
25 lines (19 loc) · 930 Bytes
/
0048-rotate-image.js
File metadata and controls
25 lines (19 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Rotate Image
* Time Complexity: O(N^2)
* Space Complexity: O(1)
*/
var rotate = function (matrix) {
const matrixLength = matrix.length;
for (let layerIndex = 0; layerIndex < Math.floor(matrixLength / 2); layerIndex++) {
const firstBound = layerIndex;
const lastBound = matrixLength - 1 - layerIndex;
for (let positionIndex = 0; positionIndex < (lastBound - firstBound); positionIndex++) {
const holdingValue = matrix[firstBound][firstBound + positionIndex];
matrix[firstBound][firstBound + positionIndex] = matrix[lastBound - positionIndex][firstBound];
matrix[lastBound - positionIndex][firstBound] = matrix[lastBound][lastBound - positionIndex];
matrix[lastBound][lastBound - positionIndex] = matrix[firstBound + positionIndex][lastBound];
matrix[firstBound + positionIndex][lastBound] = holdingValue;
}
}
};