-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJZ74.py
36 lines (34 loc) · 955 Bytes
/
JZ74.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from typing import List
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param sum int整型
# @return int整型二维数组
#
class Solution:
def FindContinuousSequence(self, sum: int) -> List[List[int]]:
output = list()
if sum < 3:
return output
if sum % 2 == 0:
nums = list(range(1, sum // 2 + 1))
else:
nums = list(range(1, sum // 2 + 2))
i, j = 0, 0
total = 0
while j < len(nums):
if total == sum:
output.append(nums[i:j])
if total <= sum:
total += nums[j]
j += 1
elif total > sum:
total -= nums[i]
i += 1
while total > sum:
total -= nums[i]
i += 1
if total == sum:
output.append(nums[i:j])
return output