杨辉三角形大家应该不陌生,其第i行恰好为 (a + b)i 的展开系数。比如上一道题的例子:
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
这道题要求直接返回第i行。题目描述为:
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
1 0 0 0 k = 0
1 1 0 0 k = 1
1 1 1 0
1 2 1 0 k = 2
1 2 1 1
1 2 3 1
1 3 3 1 k = 3
上述过程实际上就是一个in-place的迭代过程。每当生成下一行的时候,首先数组相应位置1,然后从右向左计算每一个系数。
Java代码:
public ArrayList<Integer> getRow(in