C# accepted solution


  • 0
    R
    public IList<int> SpiralOrder(int[,] matrix)
    {
    	
    	int k = 0, l = 0, last_row = matrix.GetLength(0) - 1, last_col = matrix.GetLength(1)-1;
    	var result = new List<int>();
    	if (last_col < 0 && last_row < 0) return result;	
    	
    	while (k <= last_row && l <= last_col)
    	{
    		for (int i = l; i <= last_col; i++)
    		{
    			result.Add(matrix[k,i]);
    		}
    		
    		k++;
    		for (int i = k; i <= last_row; i++)
    		{
    			result.Add(matrix[i,last_col]);
    		}
    		last_col--;
    
    		if (k <= last_row)
    		{
    			for (int i = last_col; i >= l; i--)
    			{
    				result.Add(matrix[last_row, i]);
    			}
    		}
    		last_row--;
    
    		if (l <= last_col)
    		{
    			for (int i = last_row; i >= k; i--)
    			{
    				result.Add(matrix[i, l]);
    			}
    		}
    		l++;
    
    	}
    	return result;
    	
    }

Log in to reply
 

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