119. pascal’s triangle ii

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

题意分析:
数组从后向前计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> result;
result.resize(rowIndex + 1);
for (int i = 0; i <= rowIndex; i++) {
result[i] = 1;
for (int j = i - 1;j > 0;j--)
result[j] = result[j] + result[j - 1];
result[0] = 1;
}
return result;
}
};