Java Solution using transpose


  • 0
    U
    public void rotate(int[][] matrix) {
            int n=matrix.length;
            
            int i=0,j=n-1,temp;
            
            transpose(matrix,n);
            while(i<j) {
                for(int k=0;k<n;k++) {
                temp=matrix[k][i];
                matrix[k][i]=matrix[k][j];
                matrix[k][j]=temp;
                }
                i++;
                j--;
            }
        }
        private void transpose(int[][] matrix,int n) {
            int temp;
            for(int i=0;i<n;i++) {
                for(int j=i+1;j<n;j++) {
                   temp= matrix[i][j];
                   matrix[i][j]=matrix[j][i];
                   matrix[j][i]=temp;
                }
                    
            }
        }

Log in to reply
 

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