In Place JAVA solution


  • 0
    R
        public void rotate(int[][] matrix) {
        int n = matrix.length - 1;
        int bound = matrix.length % 2 == 0 ? matrix.length / 2 - 1 : matrix.length / 2;
        int j = 0;
        while (j <= bound) {
            int i = j;
            while (i < n - j) {
                swap(matrix, j, i, i, n - j, n - i, j, n - j, n - i);
                i++;
            }
            j++;
        }
    }
    
    private void swap(int[][] matrix, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
        int val1 = matrix[i][i1];
        int val2 = matrix[i2][i3];
        int val3 = matrix[i4][i5];
        int val4 = matrix[i6][i7];
        matrix[i][i1] = val3;
        matrix[i2][i3] = val1;
        matrix[i4][i5] = val4;
        matrix[i6][i7] = val2;
    }

Log in to reply
 

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