leetcode - Pascal's Triangle

来源:互联网 发布:不用网络的监控摄像头 编辑:程序博客网 时间:2024/06/06 23:17

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> > a;        if(numRows == 0)return a;                vector<int> first;        first.push_back(1);        a.push_back(first);        for(int i = 1; i < numRows; i++)        {            vector<int> pre = a[i-1];            int n = pre.size();            vector<int> now;                        now.push_back(1);            for(int j=0; j < n-1; j++)            {                now.push_back(pre[j]+pre[j+1]);            }            now.push_back(1);            a.push_back(now);        }                return a;    }};

0 0
原创粉丝点击