Java Rotate Layer-By-Layer


  • 0
    J
    public class Solution {
        public void rotate(int[][] matrix) {
            int[][] image = matrix;
            for(int i = 0; i < image.length/2; i++) {
    			int currentLength = image.length-i-1;
    			for(int j = i; j < currentLength; j++) {
    				int top = image[i][j];
    				int right = image[j][currentLength];
    				int bottom = image[currentLength][currentLength-j+i];
    				int left = image[currentLength-j+i][i];
    				
    				image[i][j] = left;
    				image[j][currentLength] = top;
    				image[currentLength][currentLength-j+i] = right;
    				image[currentLength-j+i][i] = bottom;
    				
    				//System.out.println("Top: " + top + " | Right: " + right + " | Bottom: " + bottom + " | Left: " + left);
    			}
    			
    		}
        }
    }
    

Log in to reply
 

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