Pascal's Triangle

时间:2014-06-22 22:48:11   收藏:0   阅读:305

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]
]

解题思路:

    杨辉三角没什么好说的,注意下边界的处理即可.

解题代码:
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;
    }
};


 

Pascal's Triangle,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!