剑指 Offer 57

输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。

示例 1:
输入:target = 9
输出:[[2,3,4],[4,5]]

示例 2:
输入:target = 15
输出:[[1,2,3,4,5],[4,5,6],[7,8]]

限制:
1 <= target <= 10^5

class Solution:
    def findContinuousSequence(self, target: int) -> List[List[int]]:
        ### 双指针
        res=[]
        l=1
        r=2
        while l<r:
            a=[]
            sum=(l+r)*(r-l+1)/2
            if sum<target:
                r+=1
            elif sum>target:
                l+=1
            else:
                for i in range(l,r+1):
                    a.append(i)
                res.append(a)
                l+=1
                r+=1
        return res
原文地址:https://www.cnblogs.com/sandy-t/p/13198879.html