Pascal's Triangle II

来源:互联网 发布:数据库元组 编辑:程序博客网 时间:2024/05/19 22:49

题目描述:

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

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


思路分析:依次计算每一行的值,把它们存在同一个数组里。


代码:

class Solution{public:vector<int> getRow(int rowIndex){vector<int> result(rowIndex+1,0);result[0] = 1;for(int i = 1;i < (rowIndex+1);i++)for(int j = i;j >= 1;j--)result[j] += result[j-1];}return result;};


0 0
原创粉丝点击