pascals-triangle-ii

来源:互联网 发布:revit mep软件下载 编辑:程序博客网 时间:2024/06/05 14:09

题目:

Given an index k, return the k th 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?

程序:

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