思路
1、首先按照左上到右下的连线做对称
2、然后根据y轴做对称
(下面是另外一种对称方式,道理是一样的)
代码
class Solution { public: vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) { // write code here vector<vector<int>> res(n, vector<int>(n, 0)); for(int i = 0; i<n; i++){ for(int j = 0; j<=i; j++){ int tmp = mat[i][j]; mat[i][j] = mat[j][i]; mat[j][i] = tmp; } } // 根据y轴对称 int mid_y = (0+n-1)/2; for(int i = 0; i<n; i++){ for(int j = 0; j<=mid_y; j++){ int tmp = mat[i][j]; mat[i][j] = mat[i][n-1-j]; mat[i][n-1-j] = tmp; } } return mat; } };