ou 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?
void rotate(vector<vector<int> > &matrix) {
//顺时针旋转图片,主要是找规律,二维矩阵, (i, j)元素最后到位置(j,n - i - 1),in-place就地进行变换是说不能使用额外空间吗?
int len = matrix.size();
int count = len / 2;
for (int i = 0; i < count; ++i)
{
int end = len - i - 1;//注意end = len - i - 1;之前忘记多-1,老是出错
int row, col, temp, tt;
for (int j = i; j < end; ++j)
{
row = i;
col = j;
//下面的代码可以用循环进行优化,自己懒得优化了
temp = matrix[row][col];
swap(temp, matrix[col][len - row - 1]);
tt = col;//0
col = len - row - 1;//1
row = tt;//0
swap(temp, matrix[col][len-row - 1]);
tt = col;//1
col = len - row - 1;//1
row = tt;//1
swap(temp, matrix[col][len - row - 1]);
matrix[i][j] = temp;
}
}
}