Rotate Image [LeetCode]

时间:2023-12-29 18:52:26

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?

Solution:

     void rotate(vector<vector<int> > &matrix) {
int n = matrix.size();
for(int i = ; i < n / ; i ++) {
for(int j = i; j < n - - i; j ++){
int tmp = matrix[i][j];
matrix[i][j] = matrix[n - - j][i];
matrix[n - - j][i] = matrix[n - - i][n - - j];
matrix[n - - i][n - - j] = matrix[j][n - - i];
matrix[j][n - - i] = tmp;
}
}
}