题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
https://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
class Solution { public: vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) { for (int i = 0; i < n; ++i) { reverse(mat[i].begin(), mat[i].end()); } for (int i = 0; i < n; ++i) { for (int j = 0; i + j < n - 1; ++j) { swap(mat[i][j], mat[n - 1 - j][n - 1 - i]); } } return mat; } };
思路:翻转两次。