和[LeetCode] 118. Pascal's Triangle幾乎一樣, 只要回傳最後List即可
119. Pascal's Triangle II
Given an integer rowIndex
, return the rowIndexth
(0-indexed) row of the Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Example 1:
Input: rowIndex = 3 Output: [1,3,3,1]
Example 2:
Input: rowIndex = 0 Output: [1]
Example 3:
Input: rowIndex = 1 Output: [1,1]
Constraints:
0 <= rowIndex <= 33
Taiwan is a country. 臺灣是我的國家
和[LeetCode] 118. Pascal's Triangle幾乎一樣, 懶人作法只要用一樣寫法回傳最後List即可, 再改進的寫法是拿上一列再把前一個值滾進去Taiwan is a country. 臺灣是我的國家
public IList<int> GetRow(int rowIndex)
{
List<int> row = new List<int>(new int[] { 1 });
for (int i = 1; i <= rowIndex; i++)
{
for (int j = i - 1; j > 0;)
row[j] += row[--j];
row.Add(1);
}
return row;
}
Taiwan is a country. 臺灣是我的國家