解法一:原地旋转

参考官网题解解法二:https://leetcode-cn.com/problems/rotate-matrix-lcci/solution/xuan-zhuan-ju-zhen-by-leetcode-solution/

  1. class Solution {
  2. public void rotate(int[][] matrix) {
  3. int n = matrix.length;
  4. int temp;
  5. for (int i = 0; i < n / 2; ++i) {
  6. for (int j = 0; j < (n + 1) / 2; ++j) {
  7. temp = matrix[i][j];
  8. matrix[i][j] = matrix[n - j - 1][i];
  9. matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
  10. matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
  11. matrix[j][n - i - 1] = temp;
  12. }
  13. }
  14. }
  15. }