Skip to content

Commit d1681e1

Browse files
authored
Create 49-Rotate-Image.js
1 parent b795367 commit d1681e1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

javascript/49-Rotate-Image.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
var rotate = function(matrix) {
6+
transpose(matrix);
7+
reflect(matrix);
8+
};
9+
10+
var transpose = function(matrix) {
11+
let n = matrix.length;
12+
for (let i = 0; i < n; i++) {
13+
for (let j = i + 1; j < n; j++) {
14+
let temp = matrix[j][i];
15+
matrix[j][i] = matrix[i][j];
16+
matrix[i][j] = temp;
17+
}
18+
}
19+
}
20+
21+
var reflect = function(matrix) {
22+
let n = matrix.length;
23+
for (let i = 0; i < n; i++) {
24+
for (let j = 0; j < n / 2; j++) {
25+
let temp = matrix[i][j];
26+
matrix[i][j] = matrix[i][n - j - 1];
27+
matrix[i][n - j - 1] = temp;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)