题目
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?
算法
复杂度:O(n^2)
- 对角线swap
- 行反转
class Solution {
public:
void rotate(vector<vector<int> >& matrix) {
int n=matrix.size();
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
swap(matrix[i][j],matrix[j][i]);
for(int i=0;i<n;i++)
reverse(matrix[i].begin(),matrix[i].end());
}
};