题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
https://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
起始可以看出规律,我们可以先交换行列,然后翻转每一行。
class Solution {
public:
vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) {
//
//[1,2,3], [1 4 7]
//[4,5,6], [2 5 8]
//[7,8,9] [3 6 9]
for(int i=0;i<n;i++)
{
for(int j=0;j<i;j++)
{
swap(mat[i][j],mat[j][i]);
}
}
for(auto& e:mat)
reverse(e.begin(),e.end());
return mat;
}
};
查看18道真题和解析