Java + Very easy to follow Solution with detailed commenting


  • 0
    M

    Traverse in four directions (right,down,left,up) and peel off outer later as you traverse through the matrix in each iteration.

    public class Solution {
         public List<Integer> spiralOrder(int[][] matrix) {
    	        List<Integer> spiral = new ArrayList<>();
    	        if(matrix.length == 0){return spiral;}
    	        
    	        
    	        int rowLen = matrix.length; 
    	        int colLen = matrix[0].length;
    	        int rowMin = 0, rowMax = rowLen-1, colMin = 0, colMax = colLen-1;
    	       
                /*
                   Traverse in spiral and leave last cell for the next iteration
                   Peel one layer off every time you finish iteration.
                   In the example below group of same number cells will get picked up by each direction.
                   [1 1 1 2]
                   [4 5 6 2]
                   [4 8 7 2]
                   [4 3 3 3] 
                
                */
    	        while(rowMin< rowMax && colMin < colMax){
    	            //Spiral to right
    	            for(int col = colMin ; col <= colMax-1; col++){
    	                spiral.add(matrix[rowMin][col]);
    	            }
    	            
    	            // Spiral down
    	            for(int row = rowMin; row <= rowMax-1 ; row++){
    	                spiral.add(matrix[row][colMax]);
    	            }
    	            
    	            // Spiral left
    	            for(int col = colMax; col >= colMin+1; col--){
    	                spiral.add(matrix[rowMax][col]);
    	            }
    	            
    	            // spiral up
    	            for(int row = rowMax; row >= rowMin+1; row--){
    	                spiral.add(matrix[row][colMin]);
    	            }
    	            
    	            rowMin++;colMin++;rowMax--;colMax--;
    	        }
    	        
                /*
                    Three special cases we need to deal with
                */
             
                /*
                   1) Left with last row to deal with 
                   E.g.  [[1,2,3]] 
                            OR
                         [1  2  3  4]
                         [5  6  7  8]
                         [8  9 10 11]
                         Need to copy 6,7 in our spiral
                */
    	        if(rowMin == rowMax && colMin < colMax){
    	        	for(int col = colMin; col <= colMax; col++){
    	        		spiral.add(matrix[rowMin][col]);
    	        	}
    	        }
    	        
                  /*
                  2) Left with last column to deal with
                     Similar situation to above for vertical matrix
                  */
    	        if(colMin == colMax && rowMin < rowMax){
    	        	for(int row = rowMin; row <= rowMax; row++){
    	        		spiral.add(matrix[row][colMin]);
    	        	}
    	        }
    	        
                  /*
                    3) Deal with the center most element
                  */
    	        if((rowLen == colLen) && (rowLen%2 == 1)){
    	            spiral.add(matrix[rowLen/2][rowLen/2]);
    	        }
    	        
    	        return spiral;
    	    }
    }
    

Log in to reply
 

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