Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc23f40

Browse files
authored
Update 48-rotate-image.js
1 parent c557765 commit bc23f40

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

48-rotate-image.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,27 @@ function swap(matrix, i, j) {
4040
matrix[j][i] = matrix[i][j]
4141
matrix[i][j] = tmp
4242
}
43+
44+
// another
45+
46+
/**
47+
* @param {number[][]} matrix
48+
* @return {void} Do not return anything, modify matrix in-place instead.
49+
*/
50+
const rotate = function (matrix) {
51+
matrix.reverse()
52+
for (let i = 0; i < matrix.length; ++i) {
53+
for (let j = matrix[i].length - 1; j > i; j--) swap(matrix, i, j)
54+
}
55+
}
56+
57+
function swap(matrix, i, j) {
58+
const tmp = matrix[j][i]
59+
matrix[j][i] = matrix[i][j]
60+
matrix[i][j] = tmp
61+
}
62+
/*
63+
1 2 3 7 8 9 7 4 1
64+
4 5 6 ---> 4 5 6 --->8 5 2
65+
7 8 9 1 2 3 9 6 3
66+
*/

0 commit comments

Comments
 (0)