leetcode-Pascal's Triangle

来源:互联网 发布:各国电视台直播软件 编辑:程序博客网 时间:2024/06/17 20:38
 Difficulty:Easy

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 rowIndex) {        if(rowIndex<=0)            return vector<vector<int> >();        vector<int> ores(1,1);         vector<vector<int> > finalresult;        finalresult.push_back(ores);        if(rowIndex==1)            return finalresult;                       for(int i=1;i<rowIndex;++i){            vector<int> res;            res.push_back(1);            int osize=ores.size();            for(int i=0;i<osize-1;++i)                res.push_back(ores[i]+ores[i+1]);                res.push_back(1);            ores=res;            finalresult.push_back(ores);        }            return finalresult;         }    };


0 0
原创粉丝点击