welcome to Smartcat's cnblog

LeetCode练题——119.Pascal's Triangle II

1、题目

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

2、我的解答

 1 # -*- coding: utf-8 -*-
 2 # @Time    : 2020/3/1 21:57
 3 # @Author  : SmartCat0929
 4 # @Email   : 1027699719@qq.com
 5 # @Link    : https://github.com/SmartCat0929
 6 # @Site    : 
 7 # @File    : 119. Pascal's Triangle II.py
 8 
 9 from typing import List
10 
11 
12 class Solution:
13     def getRow(self, rowIndex: int) -> List[int]:
14         if 0 <= rowIndex <= 33:
15             c = []
16             c.append(1)
17             if rowIndex == 0:
18                 return c
19             for i in range(1, rowIndex + 1):
20                 c2 = []
21                 for j in range(i + 1):
22                     if 1 <= j < i:
23                         c2.append(c[i - 1][j - 1] + c[i - 1][j])
24                     else:
25                         c2.append(1)
26                 c.append(c2)
27             return c[rowIndex]
28         else:
29             return 0
30 
31 
32 print(Solution().getRow(0))

 

posted @ 2020-03-01 23:30  聪明猫  阅读(112)  评论(0)    收藏  举报