Pascal's Triangle
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] ]
下一行根据上一行按照规律相加就行了,old变量记录上一行。
1 class Solution { 2 public: 3 vector<vector<int>> generate(int numRows) { 4 vector<vector<int>> result; 5 if(numRows<1) return result; 6 vector<int> old; 7 for(int i=0;i<numRows;i++) 8 { 9 vector<int> f(i+1,1); 10 for(int j=1;j<i;j++) 11 { 12 f[j]=old[j-1]+old[j]; 13 } 14 result.push_back(f); 15 old = f; 16 } 17 return result; 18 } 19 };