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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int row = matrix.size();
int count = row / 2;
for (int i = 0; i <= count; ++i)
{
// start = i;
int num = row - 1 - i * 2;
for (int j = 0; j < num; ++j)
{
int tmp = matrix[i][i + j];
matrix[i][i + j] = matrix[row - 1 - i - j][i];
matrix[row - 1 - i - j][i] = matrix[row - 1 - i][row - 1 - i - j];
matrix[row - 1 - i][row - 1 - i - j] = matrix[i + j][row - 1 - i];
matrix[i + j][row - 1 - i] = tmp;
}
}
}
};