文章

LeetCode Pascal's Triangle

Problem

Given numRows, generate the first numRows of Pascal’s triangle.

For example, given numRows = 5, Return

1
2
3
4
5
6
7
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

没记错的话,这个应该叫杨辉三角型。很简单的题

Python 实现

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
27
28
29
30
31
32
33
34
35
36
37
38
'''
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]
]
'''

# author li.hzh

class Solution:
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        if numRows == 0:
            return []
        last_row = [1]
        result = [last_row]
        for row in range(2, numRows + 1):
            cur_row = [last_row[0]]
            for i in range(1, len(last_row)):
                cur_row.append(last_row[i] + last_row[i - 1])
            cur_row.append(last_row[len(last_row) - 1])
            last_row = cur_row
            result.append(cur_row)
        return result


分析

没什么可分析的,利用上一层计算出下一层的值即可以。

本文由作者按照 CC BY 4.0 进行授权