Simple Solution, O(1) space

  • 1

    We just need a temporary variable to start swapping the values at corresponding indexes in the rotate array.

        for(int i=0;i<matrix.length/2;i++) {
    		for(int j=i;j<matrix[i].length-1-i;j++) {
    			int tmp = matrix[i][j];
    			matrix[i][j] = matrix[matrix[i].length-j-1][i];
    			matrix[matrix[i].length-j-1][i] = matrix[matrix.length-1-i][matrix.length-1-j];
    			matrix[matrix.length-1-i][matrix.length-1-j] = matrix[j][matrix.length-1-i];
    			matrix[j][matrix.length-1-i] = tmp;

Log in to reply

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