题目链接在此
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
把一个n x n的2D矩阵逆时针旋转90度,不用额外空间。
那么那么上下对称翻转,再按对角线翻转即可。
值得一提的是,<algorithm>里的std::reverse函数可以翻转一个数组(vector)。
class Solution {
public:
void rotate(vector<vector<int> >& matrix) {
int n = matrix.size();
reverse(matrix.begin(), matrix.end());
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
swap(matrix[i][j], matrix[j][i]);
}
}
}
};