LEETCODE--Pascal's Triangle

来源:互联网 发布:电子仿真软件列表 编辑:程序博客网 时间:2024/06/06 04:52

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

Subscribe to see which companies asked this question

class Solution {public:    vector<vector<int>> generate(int numRows) {        vector<vector<int>> finsh;        if(numRows == 0)            return finsh;        if(numRows >= finsh.size() + 1)        {            vector<int> add;            finsh.push_back(add);        }        finsh[0].push_back(1);        if(numRows == 1)            return finsh;        else{              for(int i = 1; i < numRows; i++){                    if(i == finsh.size())                    {                        vector<int> add;                        finsh.push_back(add);                    }                     finsh[i].push_back(1);                    for(int j = 1; j <= i; j++){                        if(finsh[i].size() == i)                            finsh[i].push_back(1);                        else{                            int x = finsh[i-1][j-1];                            int y = finsh[i-1][j];                            int elem = x + y;                            finsh[i].push_back(elem);                        }                    }               }               return finsh;         }     }};
0 0