Skip to content

Commit 70432a5

Browse files
committedDec 22, 2020
feat: he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof
1 parent 1a75c6a commit 70432a5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
 

Diff for: ‎arr.he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
剑指 Offer 57 - II. 和为s的连续正数序列
6+
https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof
7+
输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。
8+
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。
9+
"""
10+
def findContinuousSequence(self, target: int) -> List[List[int]]:
11+
# 定义快指针
12+
i = 1
13+
# 定义慢指针
14+
j = 1
15+
res = []
16+
win = 0
17+
arr = list(range(1, target + 1))
18+
# 慢指针超过中值,则结果不符合
19+
while j <= target / 2:
20+
if win < target:
21+
win += i
22+
i += 1
23+
elif win > target:
24+
win -= j
25+
j += 1
26+
else:
27+
res.append(arr[j-1:i-1])
28+
win -= j
29+
j += 1
30+
return res
31+
32+
33+
so = Solution()
34+
print(so.findContinuousSequence(9))

0 commit comments

Comments
 (0)
Please sign in to comment.