Discuss / Python / 花了快两个小时搞的,和其他人的解法比起来不够简便

花了快两个小时搞的,和其他人的解法比起来不够简便

Topic source

def triangles(): n, lx, ly = 1, [], [1] while True: yield(ly) lx = ly[:] m = n + 1 ly = list(range(m)) for i in range(m): if i - 1 < 0: ly[i] = lx[i] elif i + 1 == m: ly[i] = lx[i-1] else: ly[i] = lx[i] + lx[i-1] n = n + 1

格式不太好,重新贴一下

def triangles():
    n, lx, ly = 1, [], [1]
    while True:
        yield(ly)
        lx = ly[:]
        m = n + 1
        ly = list(range(m))
        for i in range(m):
            if i - 1 < 0:
                ly[i] = lx[i]
            elif i + 1 == m:
                ly[i] = lx[i-1]
            else:
                ly[i] = lx[i] + lx[i-1] 
        n = n + 1

祭旗猫

#3 Created at ... [Delete] [Delete and Lock User]

ly = list(range(m))这一步的逻辑没看明白,为什么要有这一步?

祭旗猫

#4 Created at ... [Delete] [Delete and Lock User]

懂了,创建一个有下一层列表元素数量的列表


  • 1

Reply