Guys can you please help me to find what's wrong with this code ?


  • 0
    N

    public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
    List<Integer> numbers = new ArrayList<Integer>();

        int StartCol =0;
        int EndCol =0;
        int EndRow = 0;
        if (matrix.length > 0)
        {
             EndRow = matrix.length-1;
            
        }
            else
            {
                return numbers;
            }
    int StartRow = 0;
            if(matrix[0].length>0)
            {
             EndCol = matrix[0].length-1;
                
            }
             else
            {
                return numbers;
            }
          //  printLayer(matrix, 0, EndRow, 0, EndCol);
            while ( StartCol < EndCol || StartRow < EndRow )
            {
               for (int x : printLayer(matrix, StartRow, EndRow, StartCol, EndCol))
               {
                   numbers.add(x);
               }
                StartCol++;
                StartRow++;
                EndCol--;
                EndRow--;
    
            }
            return numbers;
    }
    
    public List<Integer> printLayer(int[][] matrix , int StartRow , int EndRow , int StartCol , int EndCol)
        {  
    

    List<Integer> numbers = new ArrayList<Integer>();
    //i for columns and j for rows
    int i =StartCol;
    int j = StartRow;
    //////////////////////////////////////
    if (StartRow == EndRow && StartCol < EndCol) // 1 row ,, many columns
    {
    while (i <= EndCol)
    {
    numbers.add(matrix[StartRow][i] );
    i++;
    }

            }
            else if (StartCol == EndCol && StartRow < EndRow)
            {
                while (j <= EndRow)
                {
                    numbers.add(matrix[j][StartCol] );
                    j++;
                }
                
            }
    
            /////////////////////////////////////
            else
            { 
            while (i <= EndCol) // Start Row fixed 
            {
               numbers.add(matrix[StartRow][ i] );
                i++;
            }
            j=StartRow+1;
            
            while (j <= EndRow) // End Col fixed
            {
                numbers.add(matrix[j][ EndCol]);
                j++;
            }
            i=EndCol-1;
            while (i >= StartCol) //End Row fixed
            {
                numbers.add(matrix[EndRow][ i] );
                i--;
            }
            j= EndRow-1;
            while (j > StartRow) // Start Col fixed
            {
                numbers.add(matrix[j][ StartCol]);
                j--;
            }
            }
            return numbers;
        }
    

    }


Log in to reply
 

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