和为target的连续正整数序列

Python Solution:

class Solution:
    def continuousSum(self, target):
        if target < 3:
            return []
        maxNum = target // 2 + 1
        nums = []
        for i in range(maxNum):
            nums.append(i+1)
        left, right = 0, 0
        res = []
        sum = 0
        while right < maxNum:
            sum += nums[right]
            right += 1

            while sum > target:
                sum -= nums[left]
                left += 1
            if sum == target:
                res.append(nums[left:right])
        return res

你可能感兴趣的:(算法)