leetcode数组汇总_LeetCode刷题实战118:杨辉三角
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
題意
給定一個非負整數?numRows,生成楊輝三角的前?numRows?行。在楊輝三角中,每個數是它左上方和右上方的數的和。樣例輸入: 5
輸出:
[
?????[1],
????[1,1],
???[1,2,1],
??[1,3,3,1],
?[1,4,6,4,1]
]
解題
class?Solution?{public:vector<vector<int>> generate(int?numRows) {vector<vector<int>> result;if?(numRows == 0) {return?{};
????}vector<int> tempRes = { 1?};//第一行,初始行
????result.push_back(tempRes);for?(int?index = 2; index <= numRows; ++index) {//利用result的最后一行進行迭代
??????tempRes = vector<int>(index, 1);//重新設定tempResfor?(int?i = 1; i < index - 1; ++i) {//利用上一行迭代下一行//result[index - 2][i - 1]上一行的第i-1個位置,圖中的左上方//result[index - 2][i]是表示上一行第i個位置,圖中的右上方
????????tempRes[i] = result[index - 2][i - 1] + result[index - 2][i];
??????}
??????result.push_back(tempRes);//此行迭代完畢放入結果
????}return?result;
??}
};
LeetCode刷題實戰113:路徑總和 II
LeetCode刷題實戰114:二叉樹展開為鏈表
LeetCode刷題實戰115:不同的子序列
LeetCode刷題實戰116:填充每個節點的下一個右側節點指針
LeetCode刷題實戰117:填充每個節點的下一個右側節點指針 II
總結
以上是生活随笔為你收集整理的leetcode数组汇总_LeetCode刷题实战118:杨辉三角的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell中返回值是1为真还是假_she
- 下一篇: python爬虫urllib 数据处理_