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

Pascal's Triangle

时间:2014-11-06 16:27:43      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   for   sp   div   on   log   

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) {
        for(int i=0;i<numRows;i++){
            if(i==0){
                vector<int> a;
                a.push_back(1);
                vet.push_back(a);
            }else{
                vector<int> a;
                for(int j=0;j<=i;j++){
                    if(j==0 || j==i)
                        a.push_back(1);
                    else{
                        a.push_back(vet[i-1][j-1]+vet[i-1][j]);
                    }
                }
                vet.push_back(a);
            }
        }
        return vet;
    }
private:
    vector<vector<int> > vet;
};

 

Pascal's Triangle

标签:style   blog   io   color   for   sp   div   on   log   

原文地址:http://www.cnblogs.com/li303491/p/4078835.html

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