-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate-image.js
More file actions
74 lines (64 loc) · 1.98 KB
/
Copy pathrotate-image.js
File metadata and controls
74 lines (64 loc) · 1.98 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Time O(n^2)
// Space O(1)
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function (matrix) {
if (!matrix || matrix.length === 0 || matrix.length !== matrix[0].length) {
throw new Error("invalid matrix");
}
if (matrix.length < 2) {
return matrix; // no need to do anything to rotate a 1,1 matrix
}
let len = matrix.length - 1,
half = Math.floor(matrix.length / 2);
// loop through diagonal
for (let layer = 0; layer < half; layer++) {
let first = layer;
let last = len - layer;
for (let i = first; i < last; i++) {
let offset = i - first;
let top = matrix[first][i]; //save top
// bottom left => top left
matrix[first][i] = matrix[last - offset][first];
// bottom right => bottom left
matrix[last - offset][first] = matrix[last][last - offset];
// top right => bottom right
matrix[last][last - offset] = matrix[i][last];
// top left => top right
matrix[i][last] = top;
}
}
return matrix;
};
// var rotate = function(matrix) {
// if (!matrix || matrix.length === 0 || matrix.length !== matrix[0].length) {
// throw new Error('invalid matrix');
// }
// if (matrix.length < 2) {
// return matrix; // no need to do anything to rotate a 1,1 matrix
// }
// let len = matrix.length - 1,
// half = Math.floor(matrix.length / 2);
// // loop through diagonal
// for (let start = 0; start < half; ++start) {
// // loop through x axis
// for (let i = 0; i < len - (start * 2); ++i) {
// let y = start,
// x = start + i,
// prev = matrix[y][x];
// // loop through all 4 corners
// for (let j = 0; j < 4; ++j) {
// let nextY = x,
// nextX = len - y,
// next = matrix[nextY][nextX];
// matrix[nextY][nextX] = prev;
// prev = next;
// x = nextX;
// y = nextY;
// }
// }
// }
// return matrix;
// }