思路:
等差数列求和
代码:
def findContinuousSequence(target: int):
res = []
# Solution 1 迭代
for i in range(1, target // 2 + 1):
length = (-2 * i + 1 + ((2 * i - 1) ** 2 + 8 * target) ** 0.5) / 2
if length - round(length) == 0:
res.append([item for item in range(i, i + round(length))])
return res
# Solution 2 循环
# for i in range(1, target // 2 + 1):
# length = (-2 * i + 1 + ((2 * i - 1) ** 2 + 8 * target) ** 0.5) / 2
# if length - round(length) == 0:
# temp = []
# for j in range(i, i + round(length)):
# temp.append(j)
# res.append(temp)
# return res
# Solution 3 双迭代
# res = []
# for i in range(1, target // 2 + 1):
# length = (-2 * i + 1 + ((2 * i - 1) ** 2 + 8 * target) ** 0.5) / 2
# res.append([item for item in range(i, i + round(length)) if length - round(length) == 0])
# return [result for result in res if result != []]
total = 9
print(findContinuousSequence(total))