From outside to inside AC Java code


  • 0
    Y
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        int start = 0;
        while ( start < n/2 ){  //till the middle
            int[] tmp = new int[n-1-start*2];
            
            for (int i = 0; i < tmp.length; i++){
                tmp[i] = matrix[start+i][n-1-start]; //save left
                matrix[start+i][n-1-start] = matrix[start][i+start]; //move top to left
                matrix[start][i+start] = matrix[n-1-start-i][start]; // move right to top
                matrix[n-1-start-i][start] = matrix[n-1-start][n-1-start-i]; //move bottom to right
                matrix[n-1-start][n-1-start-i] = tmp[i]; //move left to bottom
            }
            start++;
        }
        
    }

Log in to reply
 

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