一、题目
二、示例
三、代码
class Solution:
def generate(self , num: int) -> List[List[int]]:
if num == 0:
return []
result = [[1]]
for i in range(1, num):
temp = [1]
for j in range(1, i):
temp.append(result[i-1][j-1] + result[i-1][j])
temp.append(1)
result.append(temp)
return result
四、算法说明
- 先排除
num
为0的情况; - 根据杨辉三角的核心公式
result[i][j] = result[i-1][j-1] + result[i-1][j]
得到每一层的元素,并进将每一层的元素,存入result
中; - 打印出输出结果。
胡萝卜
2022年8月25日
我不知道将去向何方,但我已在路上! |
---|
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! |