首页 > 代码库 > Pascal's Triangle

Pascal's Triangle

Given numRows, generate the firstnumRows 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]
]解题思路:    杨辉三角没什么好说的,注意下边界的处理即可.解题代码:
class Solution {
public:
    vector<vector<int> > generate(int numRows) 
    {
        vector<vector<int> > res;
        for (int i = 0; i < numRows; ++i)
        {
            res.push_back(vector<int>());
            res[i].push_back(1);
            for (int j = 1; j < i; ++j)
                res[i].push_back(res[i-1][j]+res[i-1][j-1]);
            if (i)
                res[i].push_back(1);
        }
        return res;
    }
};