48. Rotate Image
非In-place很好做,就是纸上画一个然后转一下举个例子
1 public void rotate(int[][] matrix) { 2 if(matrix == null || matrix.length == 0) { 3 return; 4 } 5 int n = matrix.length; 6 int[][] res = new int[n][n]; 7 for(int i = 0; i < n; i++) { 8 for(int j = 0; j < n; j++) { 9 res[j][n - 1 - i] = matrix[i][j]; 10 } 11 } 12 for(int i = 0; i < n; i++) { 13 for(int j = 0; j <n; j++) { 14 matrix[i][j] = res[i][j]; 15 } 16 } 17 }
in-place: