[LeetCode] Rotate Image

48. Rotate Image

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

1
2
3
4
5
6
7
8
9
class Solution {
public:
void rotate(vector<vector<int>>& m) {
reverse(begin(m), end(m));
for(int i = 0; i < m.size(); i++)
for(int j = i + 1; j < m.size(); j++)
swap(m[i][j],m[j][i]);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/19/PS/LeetCode/rotate-image/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.