``````class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
vector<vector<int>> res;
int n = matrix.size();
for(int i = 0;i < n;i++)
{
vector<int> line;
for(int j = n - 1;j >= 0;j--)
{
line.push_back(matrix[j][i]);
}
res.push_back(line);
}

swap(res,matrix);
}
};``````

``````执行结果：

#### 原地翻转

``````class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = 0;i < n/2;i++)
{
for(int j = 0;j < (n + 1)/2;j++)
{
int temp = matrix[i][j];

matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];

matrix[j][n-1-i] = temp;
}
}
}
};``````

#### 两次翻转

``````class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
//水平翻转
int n = matrix.size();
for(int i = 0;i < n/2;i++)
swap(matrix[i],matrix[n-1-i]);

for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++)
{
cout << matrix[i][j] <<" ";
}

cout << endl;
}

//对角线翻转
for(int i = 0;i < n;i++)
{
for(int j = i + 1;j < n;j++)
{
swap(matrix[i][j],matrix[j][i]);
}
}
}
};``````

``````Runtime: 0 ms, faster than 100.00% of C++ online submissions for Rotate Image.
Memory Usage: 7 MB, less than 95.80% of C++ online submissions for Rotate Image.``````