leetCode:Pascal's Triangle

来源:互联网 发布:淘宝纯银首饰 编辑:程序博客网 时间:2024/06/05 22:42

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]]
#include<iostream>#include<vector>using namespace std;class Solution{public:    vector<vector<int> > generate(int numRows){        vector<vector<int> > temp;        if(numRows<=0) return temp;        for(int i=0;i<numRows;i++){            vector<int> row(i+1);            row[0]=row[i]=1;            for(int j=1;j<i;j++)//if j>i;the for won't start                row[j]=temp[i-1][j-1]+temp[i-1][j];            temp.push_back(row);        }        return temp;    }//generate};int main(){    Solution sol;    vector<vector<int> > temp=sol.generate(5);    int len=temp.size();    for(int i=0;i< len;i++){        vector<int>:: iterator iter;        for(iter=temp[i].begin();iter!=temp[i].end();iter++){            cout<<*iter<<" ";        }        cout<<"\n"<<endl;    }return 1;}






                                             
0 0
原创粉丝点击