首页 > 代码库 > Pascal's Triangle

Pascal's Triangle

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