leetcode 刷题 118. 杨辉三角解题思路
生活随笔
收集整理的這篇文章主要介紹了
leetcode 刷题 118. 杨辉三角解题思路
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個非負整數?numRows,生成楊輝三角的前?numRows?行。
在楊輝三角中,每個數是它左上方和右上方的數的和。
示例:
輸入: 5
輸出:
[
? ? ?[1],
? ? [1,1],
? ?[1,2,1],
? [1,3,3,1],
?[1,4,6,4,1]
]
解答:
class Solution:def generate(self, numRows: int) -> List[List[int]]:result = [ [1] * (i+1) for i in range(numRows)]if numRows>=3:for i in range(2,numRows):for j in range(1,i):result[i][j] = result[i-1][j-1] + result[i-1][j]return result?
總結
以上是生活随笔為你收集整理的leetcode 刷题 118. 杨辉三角解题思路的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu16.04安装ssh服务,远
- 下一篇: leetcode 刷题 119. 杨辉三