Simple solution Java


  • 1
    R
    public class Solution {
        public void rotate(int[][] matrix) {
            
            int length = matrix[0].length;
            for (int layer = 0; layer < length / 2; layer++) {
                rotation_helper(matrix, layer);
            }
            
        }
        
        
        public void rotation_helper(int[][] m, int layer) {
            int length = m.length;
            int first = layer;
            int last = length - 1 - layer;
            
            for (int k = first; k < last; k++) {
                
                int offset = k - first;
                
                // temp <- top
                int temp = m[first][k];
                
                
                // top <- left
                m[first][k] = m[last - offset][first];
                
                // left <- bottom
                
                m[last - offset][first] = m[last][last - offset];
                
                // bottom <- right
                
                m[last][last - offset] = m[k][last];
                
                
                // right <- top
                m[k][last] = temp;
                
                
            }
            
        }
    }
    
    
    
    
    
    
    

Log in to reply
 

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