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]
]
分析:大概是考察用 vector 创建二维数组的应用~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<int>> v(numRows); if(numRows == 0) return v; for(int i = 0; i < numRows; i++) { v[i].resize(i + 1); } v[0][0] = 1; if(numRows == 1) return v; v[1][0] = 1; v[1][1] = 1; for(int i = 2; i < numRows; i++) { v[i][0] = 1; v[i][i] = 1; } for(int i = 2; i < numRows; i++) { for(int j = 1; j < i; j++) { v[i][j] = v[i - 1][j - 1] + v[i - 1][j]; } } return v; } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼