pascals-triangle leetcode C++

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5, Return

[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]

C++

class Solution {
public:
    vector<vector<int> > generate4(int numRows) {
        vector<vector<int>> dp(numRows);
        if (numRows < 1) return dp;
        dp[0].push_back(1);
        for (int i =1; i < numRows; i++){
            dp[i].push_back(1);
            for(int j = 1; j < i; j++)
                dp[i].push_back(dp[i-1][j-1] + dp[i-1][j]);
            dp[i].push_back(1);
        }
        return dp;
    }

    vector<vector<int>> generate(int numRows){
        vector<vector<int>> dp(numRows);
        for(int i = 0; i < numRows; ++i){
            dp[i].push_back(1);
            for(int j = 1; j < i; ++j)
                dp[i].push_back(dp[i-1][j-1] + dp[i-1][j]);
            if (i>0)
                dp[i].push_back(1);
        }
        return dp;
    }
};

 

posted @ 2018-09-22 10:58  vercont  阅读(142)  评论(0编辑  收藏  举报