题目:
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
这道题大意就是将所给矩阵顺时针旋转90°,我感觉最简单粗暴的办法就是复制一个新的矩阵,然后填进去。基本填法就是将每一列倒着填入每一行就行了。代码如下:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
vector<vector<int>>rotate_matrix(matrix);
for(int i=0; i<rotate_matrix.size(); i++){
for(int j=0; j < rotate_matrix.size(); j++)
{
matrix[j][i] = rotate_matrix[rotate_matrix.size() - i - 1][j];
}
}
}
};