题目描述
blablabla
等差数列求和
设等差数列有$k$项,首项为$a_1$,$a_k=a_1+k-1$,则有$\frac{a_1+a_k}{2}\cdot k=sum$,即$a_1 = \frac{2\cdot sum-k^2+k}{2k}$。
根据题意有$a_1\geq 1, k\geq 2$。根据$k$,寻找$a_1$的整数解。
class Solution(object):
def findContinuousSequence(self, sum):
"""
:type sum: int
:rtype: List[List[int]]
"""
if sum < 3:
return []
def get(k):
return (2*sum-k*k+k) / (2*k)
res = []
k = 2
while True:
a = get(k)
if a < 1:
break
if a - int(a) == 0:
a = int(a)
res.append(list(range(a, a + k)))
k += 1
return res