Skip to main content

模拟专题

矩阵模拟

矩阵镜像

tip

按行进行翻转即可,用双指针辅助

void mirror(vector<string> &s)
{
for(int i = 0;i < n;i++)
for(int j = 0, k = n-1;j < k;j++,k--)
swap(s[i][j],s[i][k]);
}

矩阵旋转

tip

思路:先按照对角线翻转,再按照水平镜像翻转

void rotate(vector<string> &s)
{
for (int i = 0; i < n; i ++ )
for (int j = 0; j < i; j ++ )
swap(s[i][j],s[j][i]);
mirror(s);
}