先左右对称反转,再沿右对角线对折,面试记住就行了
代码:
1 class Solution { 2 public: 3 void rotate(vector<vector<int>>& matrix) { 4 int len = matrix.size(); 5 //左右翻 6 for(int i=0;i<len;i++){ 7 for(int j=0;j<len/2;j++){ 8 int temp = matrix[i][j]; 9 matrix[i][j] = matrix[i][len-1-j]; 10 matrix[i][len-1-j] = temp; 11 } 12 } 13 //对角线翻转 14 for(int i=0;i<len;i++){ 15 for(int j=0;j<len-i;j++){ 16 int temp = matrix[i][j]; 17 matrix[i][j] = matrix[len-1-j][len-1-i]; 18 matrix[len-1-j][len-1-i] = temp; 19 } 20 } 21 } 22 };
来源:https://www.cnblogs.com/FdWzy/p/12334155.html