Skip to content

Commit 5acdbbf

Browse files
authored
Create 1886-determine-whether-matrix-can-be-obtained-by-rotation.js
1 parent e8f664c commit 5acdbbf

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @param {number[][]} target
4+
* @return {boolean}
5+
*/
6+
const findRotation = function(mat, target) {
7+
if(chk(mat, target)) return true
8+
for(let i = 0; i < 3; i++) {
9+
rotate(mat)
10+
if(chk(mat, target)) return true
11+
}
12+
return false
13+
};
14+
15+
function chk(m1, m2) {
16+
for(let i = 0; i < m1.length; i++) {
17+
for(let j = 0; j < m1.length; j++) {
18+
if(m1[i][j] !== m2[i][j]) return false
19+
}
20+
}
21+
return true
22+
}
23+
24+
function rotate(matrix) {
25+
matrix.reverse()
26+
for (let i = 0; i < matrix.length; ++i) {
27+
for (let j = matrix[i].length - 1; j > i; j--) swap(matrix, i, j)
28+
}
29+
}
30+
31+
function swap(matrix, i, j) {
32+
const tmp = matrix[j][i]
33+
matrix[j][i] = matrix[i][j]
34+
matrix[i][j] = tmp
35+
}

0 commit comments

Comments
 (0)