Java simple code


  • 0
    public class Solution {
        public void rotate(int[][] matrix) {
            if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
                return;
            int tmp;
            int n = matrix.length;
            for (int i = 0; i <= n / 2 - 1; i++)
                for (int j = 0; j <= (n - 1) / 2; j++) {
                    tmp = matrix[i][j];
                    matrix[i][j] = matrix[n - 1 - j][i];
                    matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
                    matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
                    matrix[j][n - 1 - i] = tmp;
                }
        }
    }

  • 0
    J

    Very good solution!, thanks!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.