您的位置:首页 > 其它

[Leetcode] Pascal's Triangle II

2016-09-30 11:06 274 查看
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]
.

Note:

Could you optimize your algorithm to use only O(k) extra space?

Subscribe to see which companies asked this question

这个问题要求储存空间要小,可以采用递归的方法去处理

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> cur(rowIndex+1,1);
if(rowIndex == 0){
return cur;
}
vector<int> last=getRow(rowIndex-1);
for(int i = 0;i<cur.size();i++){
if(i != 0 && i != cur.size()-1) {
cur[i]=last[i]+last[i-1];
}
}
return cur;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Leetcode