C# - basic


  • 0
        public IList<IList<int>> Generate(int numRows) 
        {
            IList<IList<int>> rows = new List<IList<int>>(numRows);
            
            for (int row = 1; row <= numRows; row++)
            {
                IList<int> rowList = new List<int>(new int[row]);
                rows.Add(rowList);
                
                for (int index = 0; index < row; index++)
                {
                    if (index == 0 || index == row - 1)
                    {
                        rowList[index] = 1;
                    }
                    else
                    {
                        rowList[index] = rows[row - 2][index] + rows[row - 2][index - 1];
                    }
                }
            }
            
            return rows;
        }
    

Log in to reply
 

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