Pascal's Triangle

Pascal's Triangle

 Total Accepted: 38612 Total Submissions: 126553

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>> V(numRows);
        int i,j,c;
        for(i=0;i<numRows;i++)
        {
            for(j=0;j<=i;j++)
            {
                if(j==0||j==i)
                    c=1;
                else c=V[i-1][j]+V[i-1][j-1];
                V[i].push_back(c);
            }
        }
        return V;
    }
};

Show Tags

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-03-18 19:43  Thereisnospon  阅读(115)  评论(0编辑  收藏  举报