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?
分析:找规律题目,从最外圈开始交换, 对于第一圈,考虑 A[0][0]-A[0][n-2]的交换情况。
class Solution {
public:
void rotate(vector<vector<int> > &A) {
int n = A.size();
if(n == 0) return ;
for(int i = 0; i < n / 2; i++) {
for(int j = i; j < n - 1 - i; j++) {
int tmp = A[i][j];
A[i][j] = A[n-1-j][i];
A[n-1-j][i] = A[n-1-i][n-1-j];
A[n-1-i][n-1-j] = A[j][n-1-i];
A[j][n-1-i] = tmp;
}
}
}
};