模拟矩阵旋转\(90°\),老套路题了。
class Solution {
public:
int n;
bool is_equal(vector<vector<int>>& a, vector<vector<int>>& b)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(a[i][j] != b[i][j])
return false;
return true;
}
void rotate(vector<vector<int>> &mat)
{
vector<vector<int>> temp(n,vector<int>(n));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
temp[j][n-1-i]=mat[i][j];
mat=temp;
}
bool findRotation(vector<vector<int>>& mat, vector<vector<int>>& target) {
n=mat.size();
if(is_equal(mat,target)) return true;
for(int i=0;i<3;i++)
{
rotate(mat);
if(is_equal(mat,target)) return true;
}
return false;
}
};
原文:https://www.cnblogs.com/fxh0707/p/14856865.html