https://leetcode-cn.com/problems/pascals-triangle/
解法1:迭代
思路:
每一行的第一列和最后一列为1,其余列都是上一行对应列和上一行对应列-1之和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<int>> res(numRows); for(int i=0;i<numRows;i++) { res[i].push_back(1); for(int j=1;j<i+1;j++) { if(j==i) { res[i].push_back(1); } else { res[i].push_back(res[i-1][j-1]+res[i-1][j]); } } } return res; } };
|