题目链接:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/
难度:简单
描述:
输入一个正整数 target
,输出所有和为 target
的连续正整数序列(至少含有两个数)。
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。
题解
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
ret = []
temp_sum = 0
left = 1
for i in range(1, target):
temp_sum += i
while temp_sum > target:
temp_sum -= left
left += 1
if temp_sum == target:
ret.append(list(range(left, i+1)))
return ret