Leetcode 118. 杨辉三角 (每日一题 20210901)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 118. 杨辉三角 (每日一题 20210901)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個非負整數?numRows,生成「楊輝三角」的前?numRows?行。在「楊輝三角」中,每個數是它左上方和右上方的數的和。示例 1:輸入: numRows = 5
輸出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
示例?2:輸入: numRows = 1
輸出: [[1]]鏈接:https://leetcode-cn.com/problems/pascals-triangleclass Solution:def generate(self, numRows: int) -> List[List[int]]:res = []for i in range(numRows):tmp = [1] * (i+1)for j in range(1, i):tmp[j] = res[i-1][j-1] + res[i-1][j]res.append(tmp)return res
總結
以上是生活随笔為你收集整理的Leetcode 118. 杨辉三角 (每日一题 20210901)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 31. 下一个排列 (
- 下一篇: Leetcode 142. 环形链表 I