Determine Whether Matrix Can Be Obtained By Rotation LeetCode solution in Python

1
class Solution:
2
def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:
3

4
# if already equal
5
if target == mat:
6
return True
7
# there are 4 different rotation with 90 deg.
8
# We need to check at most 3 more rotation.
9
for i in range(3):
10
# transpose the matrix by swap row and col values.
11
for j in range(len(mat)):
12
for k in range(j + 1, len(mat)):
13
mat[j][k], mat[k][j] = mat[k][j], mat[j][k]
14
# Reflect the row by reverse it.
15
mat[j] = mat[j][::-1]
16
# now the matrix is roteted; check if they're alike.
17
if target == mat:
18
return True
19
return False

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0