Java most clear solution


  • 0
    public class Solution {
        public void rotate(int[][] matrix) {
            int n=matrix.length;
            int layer = n/2;
            for(int i=0;i<n;++i){
                int s = i; // start
                int e = n-1-i; // end
                for(int j=s, k=e;j<e;++j,--k){ //j from s to e, k from e to s
                    int tmp=matrix[s][j];
                    matrix[s][j]=matrix[k][s];
                    matrix[k][s]=matrix[e][k];
                    matrix[e][k]=matrix[j][e];
                    matrix[j][e]=tmp;
                }
            }
        }
    }

Log in to reply
 

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