My Java Solution- using swap,constance space

  • 0
    public class Solution {
        public void rotate(int[][] matrix) {
            if(matrix==null) return ;
            int row=matrix.length;        
            for(int i=0;i<row/2;i++){
                for(int j=i;j<row-i-1;j++){
                 swap(matrix,i,j,row-1-j,i);  // an interesting thing here is that : j+(row-1-j)=row-1
                 swap(matrix,row-1-j,i,row-1-i,row-1-j); // i+(row-1-i)=row-1
                 swap(matrix,row-1-i,row-1-j,j,row-1-i); // row-1-j+j=row-1
     public void swap(int[][] matrix,int i,int j,int p,int q){
            int tmp=matrix[i][j];

Log in to reply

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