LeetCode(Python Edition)-。 118パスカルの三角形
Leetcode
負でない整数が与えられた numRows 、最初の生成 numRows パスカルの三角形の。
パスカルの三角形では、各数値はそのすぐ上の2つの数値の合計です。
例:
入力: 5 出力: [ [1]、 [1,1]、 [1,2,1]、 [1,3,3,1]、 [1,4,6,4,1] ] |
class Solution(object): def generate(self, numRows): ''' :type numRows: int :rtype: List[List[int]] ''' if numRows == 0: return [] if numRows == 1: return [[1]] if numRows == 2: return [[1], [1, 1]] triangle = [[1], [1, 1]] for i in range(3, numRows + 1): pre = triangle[i - 2] current = [1] for j in range(i - 2): current.append(pre[j] + pre[j + 1]) current.append(1) triangle.append(current) return triangle