标签:
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> #include<algorithm> using namespace std; vector<vector<int> > generate(int numRows) { vector<vector<int>> Result; if (numRows == 0) return Result; vector<int> firstrow(1, 1); Result.push_back(firstrow); for (int i = 1; i != numRows;++i) { vector<int> Irows(i + 1, 1); for (int j = 1; j != i;++j) Irows[j] = Result.back()[j - 1] + Result.back()[j]; Result.push_back(Irows); } return Result; }
标签:
原文地址:http://blog.csdn.net/li_chihang/article/details/44497677