Skip to content

Latest commit

 

History

History
67 lines (45 loc) · 1.31 KB

118 杨辉三角I.md

File metadata and controls

67 lines (45 loc) · 1.31 KB

118 杨辉三角I

一、题目

给定一个非负整数numRows,生成杨辉三角的前numRows行。

image

在杨辉三角中,每个数是它左上方和右上方的数的和。

  • 示例:
输入: 5
输出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

二、Python3程序

# -*- coding:utf-8 -*-
# &Author  AnFany

# 118_Pascal's_Triangle_I 杨辉三角I


class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        
        # 当行数为0时
        if not numRows:
            return []
        
        # 开始的行数
        start_num = 1

        # 存储最终的结果
        result = [[1]]  # 等于1时的结果

        while start_num < numRows:
            last_row = result[-1]

            # 前面加0元素构成的列表
            forward_list = [0] + last_row

            # 后面加0元素构成的列表
            back_list = last_row + [0]

            # 两个列表对元素求和即可得到新的行
            new_row = [i + j for i, j in zip(forward_list, back_list)]

            # 添加到结果中
            result.append(new_row)

            # 行数加1
            start_num += 1

        return result