递归秒了求优化
查看原帖
递归秒了求优化
1235355
Knd_zpy楼主2024/6/3 19:37
def generate(numRows):
    if numRows == 0:
        return []
    if numRows == 1:
        return[[1]]
    if numRows == 2:
        return [[1], [1, 1]]
    numRows -= 2
    rList = [[1], [1, 1]]
    while numRows > 0:
        newList = [1]
        for i in range(len(rList[-1])-1):
            newList.append(rList[-1][i]+rList[-1][i+1])
        newList.append(1)
        rList.append(newList)
        numRows -= 1
    return(rList)


n = int(input())
lst = generate(n)
for i in range(len(lst)):
    for j in range(len(lst[i])):
        print(lst[i][j],end=" ")
    print()

就写个函数嘛简单

又觉得没什么没必要啊

哪位大佬优化一下?

2024/6/3 19:37
加载中...