Skip to main content

48. Rotate Image

class Solution {
public:
void rotate(vector<vector<int>>& matrix)
{
int n = matrix.size();
for(int i = 0; i < n; ++i)
{
for(int j = i; j < n; ++j)
{
swap(matrix[i][j], matrix[j][i]);
}
}
for(int i = 0; i < n; i++)
{
reverse(matrix[i].begin(), matrix[i].end());
}
}
};
  • T: O(MN)O(M \cdot N)
  • S: O(1)O(1)