-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_4.py
More file actions
47 lines (41 loc) · 920 Bytes
/
question_4.py
File metadata and controls
47 lines (41 loc) · 920 Bytes
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
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/2 19:06
# @Author : cancan
# @File : question_4.py
# @Function : 杨辉三角
"""
Question:
给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
Example:
输入: 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]]
res = [[1]]
r = [1, 1]
for i in range(1, numRows):
res.append(r)
t = [1]
for x in range(len(r) - 1):
t.append(r[x] + r[x + 1])
t.append(1)
r = t
return res