JAVA Code, With Comment, In Place Rotate


  • 0
    X

    public class Solution {
    public void rotate(int[][] matrix) {
    for(int i = 0; i < matrix.length / 2; i++) {
    for (int j = i; j < matrix.length - i - 1; j++) {
    int tmp = matrix[i][j]; // tmp = top
    matrix[i][j] = matrix[matrix.length - j - 1][i]; // top = left
    matrix[matrix.length - j - 1][i] = matrix[matrix.length - i - 1][matrix.length - 1 - j]; // left = bottom
    matrix[matrix.length - i - 1][matrix.length - 1 - j] = matrix[j][matrix.length - i - 1]; // bottom = right
    matrix[j][matrix.length - i - 1] = tmp; // right = tmp
    }
    }
    }
    }


Log in to reply
 

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