码迷,mamicode.com
首页 > 其他好文 > 详细

Pascal's Triangle

时间:2014-06-22 22:48:11      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:leetcode

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

Pascal's Triangle

标签:leetcode

原文地址:http://blog.csdn.net/dream_you_to_life/article/details/32715043

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!