?
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?
基本思路:
内层循环,使用递减方式。
因为计算当前值时,需要用到上一行的当前列和前一列。
如果从前向后的话,需要额外使用一个变量,来保存前一列的历史值。
?
?
class Solution {
public:
vector
getRow(int rowIndex) {
vector
ans(rowIndex+1, 1); for (int i=0; i<=rowIndex; i++) { for (int j=i-1; j>0; j--) { ans[j] += ans[j-1]; } } return ans; } };
?