Easy Java Solution using layer padding [Single nested-loop]


  • 0
    S
    public class Solution {
        public void rotate(int[][] matrix) {
            if(matrix == null || matrix.length == 0){
                return;
            }
            int layers = matrix.length/2, layer = 0;
            int n = matrix.length - 1;
            
            while(layer < layers) {
                for(int i = layer; i < n - layer; i++){
                    int temp = matrix[layer][i], pad = n-layer;
                    matrix[layer][i] = matrix[n-i][layer];
                    matrix[n-i][layer] = matrix[pad][n-i];
                    matrix[pad][n-i] = matrix[i][pad];
                    matrix[i][pad] = temp;
                }
                layer++;
            }
            return;
        }
    }

Log in to reply
 

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