题目描述:
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?
代码:
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
//先左右翻转
int N = matrix.size();
for (int i = 0; i < N;i++)
for (int j = 0; j < N / 2; j++)
swap(matrix[i][j], matrix[i][N - 1 - j]);
//再以右斜线为轴翻转
for (int i = 0; i < N;i++)
for (int j = 0; j < N - 1 - i; j++)
swap(matrix[i][j], matrix[N - 1 - j][N - 1 - i]);
}
};