Leetcode Pascal's Triangle

来源:互联网 发布:电子书转换格式软件 编辑:程序博客网 时间:2024/06/05 09:21

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>> result;        vector<int > temp;        if(numRows==0)            return result;        temp.push_back(1);        result.push_back(temp);        temp.clear();        if(numRows==1)            return result;        temp.push_back(1);        temp.push_back(1);        result.push_back(temp);        if(numRows==2)            return result;        if(numRows>2)        {            vector<int>temp1;                        for(int i=3; i<=numRows; i++)            {                temp1.push_back(1);                for(int j=1; j<i-1; j++)                {                    temp1.push_back(temp[j]+temp[j-1]);                }                temp1.push_back(1);                result.push_back(temp1);                temp.clear();                temp = temp1;                temp1.clear();            }        }        return result;    }};


0 0