/*
48. Rotate Image My Submissions QuestionEditorial Solution
Total Accepted: 66533 Total Submissions: 192145 Difficulty: 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?
Subscribe to see which companies asked this question
Show Tags
Have you met this question in a real interview? Yes No
Discuss
*/
/*
解题思路:
1.将矩阵沿副对角线对折交换
2.将上一步得到的矩阵按行对折交换
*/
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
//首先将矩阵沿副对角线进行交换
int n=matrix.size();
if(n==0)return ;
for(int i=0;i<n;i++){
for(int j=0;j<n-i;j++){
swap(matrix[i][j],matrix[n-j-1][n-i-1]);
}
}
//将矩阵行对称调换
for(int i=0;i<n/2;i++){
swap(matrix[i],matrix[n-i-1]);
}
}
};