1.6 Given an image represented by an N*N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
void rotate(vector<vector<int> >& matrix, int n) {
for (int layer = 0; layer < n/2; layer++) {
int first = layer;
int last = n - layer - 1;
for (int i = first; i < last; i++) {
int offset = i - first;
int tmp = matrix[first][i];
matrix[first][i] = matrix[last-offset][first];
matrix[last-offset][first] = matrix[last][last-offset];
matrix[last][last-offset] = matrix[i][last];
matrix[i][last] = tmp;
}
}
}