您的位置:首页 > 其它

[leetcode] 119. Pascal's Triangle II

2016-07-25 07:50 537 查看
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?

解法一:

这道题的关键在于只能使用O(k)的space。其实产生pascal triangle是有规律的。具体实现见code。
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ret;

for(int i=0;i<rowIndex+1;i++){
for(int j=ret.size()-2; j>=0; j--){
ret[j+1] += ret[j];
}
ret.push_back(1);
}

return ret;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  easy leetcode