给定非负索引k,其中k≤33,返回杨辉三角形的第k个索引行。
样例1
输入: 3
输出: [1,3,3,1]
样例2
输入: 4
输出: [1,4,6,4,1]
挑战
你可以优化你的算法到空间复杂度为O(k)吗?
注意事项
注意行下标从 0 开始
在杨辉三角中,每个数字是它上面两个数字的总和。
class Solution {
public:
/**
* @param rowIndex: a non-negative index
* @return: the kth index row of the Pascal's triangle
*/
vector<int> getRow(int numRows) {
// write your code here
vector<vector<int>> result(numRows+1);
for (int i = 0; i <= numRows; i++) {
/* code */
result[i].push_back(1);
for (int j = 1; j <= i; j++) {
/* code */
if(j==i) result[i].push_back(1);
else result[i].push_back(result[i-1][j-1]+result[i-1][j]);
}
}
return result[numRows];
}
};