Determine Whether Matrix Can Be Obtained By Rotation LeetCode solution in C++

1
class Solution {
2
public:
3
bool findRotation(vector<vector<int>> &mat, vector<vector<int>> &target) {
4
int n = mat.size();
5
if (mat == target) { // rotation by 0 degree.
6
return true;
7
}
8

9
int deg = 3; // more rotations with 90, 180, 270 degree's.
10

11
while (deg--) {
12
for (int i = 0; i < n; i++) {
13
for (int j = i; j < n; j++) {
14
swap(mat[i][j], mat[j][i]); // transpose of matrix.
15
}
16
}
17
for (int i = 0; i < n; i++) {
18
reverse(mat[i].begin(), mat[i].end()); // reverse each row.
19
}
20
if (mat == target) {
21
return true;
22
}
23
}
24
return false;
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0