Discuss / Python / 对奇数行偶数行分别处理,减少加法次数

对奇数行偶数行分别处理,减少加法次数

Topic source
from itertools import countdef tri():    L = [1]    for line in count():        if line % 2 == 0:            yield L + L[:-1][::-1]        else:            yield L + L[::-1]            L.append(L[-1])        L = [1] + [L[i] + L[i + 1] for i in range(len(L) - 1)]

# 可以节省一半的时间

  • 1

Reply