您的位置:首页 > 其它

leetcode-面57 和为s的连续正数序列

2020-03-15 12:14 447 查看

方法一:数学方法

class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
res = []
for n in range(2, target - 1):
temp = target - n*(n-1)//2
if temp <= 0:
break
if not temp % n:
a1 = temp // n
res.append([a1 + i for i in range(n)])
return res[::-1]```

方法二:滑动窗口法

```python
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
i, j, sum = 1, 1, 0
res = []
while i <= target // 2:
if sum < target:
sum += j
j += 1
elif sum > target:
sum -= i
i += 1
else:
t = list(range(i,j))
res.append(t)
sum -= i
i += 1
return res
  • 点赞
  • 收藏
  • 分享
  • 文章举报
Xenonon 发布了32 篇原创文章 · 获赞 0 · 访问量 281 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: