Leetcode 48. Rotate Image (Medium) (cpp)
Tag: Array
Difficulty: Medium
/*
48. Rotate Image (Medium)
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) {
int n = matrix.size() - 1;
for (int i = 0; i < (n + 2) / 2; i++) {
for (int j = i; j < n - i; j++) {
swap(matrix[i][j], matrix[j][n - i]);
swap(matrix[i][j], matrix[n - i][n -j]);
swap(matrix[i][j], matrix[n - j][i]);
}
}
}
};