Java solution rotating from periphery to kernel


  • 0
    K

    Not a fast one. Just provide with an alternative.

    public class Solution {
        public void rotate(int[][] matrix) {
            int n=matrix.length;
            if(n<2) return;
            int start=0, end=n-1;
            while(end-start>=1) {
                for(int i=start; i<end; i++) {
                    int tmp=matrix[start][i];
                    matrix[start][i]=matrix[end-i+start][start];
                    matrix[end-i+start][start]=matrix[end][end-i+start];
                    matrix[end][end-i+start]=matrix[i][end];
                    matrix[i][end]=tmp;
                }
                start++;
                end--;
            }
            return;
        }
    }

Log in to reply
 

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