先按照对角线翻转,再按照x轴翻转
1 public class Solution { 2 public void rotate(int[][] matrix) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len = matrix.length; 6 if(len == 0) 7 return; 8 9 for(int i = 0; i < len - 1; i++){ 10 for(int j = 0; j < len -i; j++){ 11 swap(matrix, i, j, len - 1 - j, len - 1 -i); 12 } 13 } 14 15 16 for(int i = 0; i < len / 2; i++){ 17 for(int j = 0; j < len; j++){ 18 swap(matrix, i, j, len - 1 -i, j); 19 } 20 } 21 } 22 23 public void swap(int[][] matrix, int i, int j, int m, int n){ 24 int tmp = matrix[i][j]; 25 matrix[i][j] = matrix[m][n]; 26 matrix[m][n] = tmp; 27 } 28 }