-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest57_和为s的数字.py
38 lines (37 loc) · 1.11 KB
/
test57_和为s的数字.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
37
38
#1 和为s的两个数字
class Solution:
def FindNumbersWithSum(self, array, tsum):
# write code here
if not array:
return []
left, right = 0, len(array) - 1
while left <= right:
temp = array[left] + array[right]
if temp == tsum:
return [array[left], array[right]]
elif temp < tsum:
left += 1
else:
right -= 1
return []
#2 和为s的连续正数序列
class Solution:
def FindContinuousSequence(self, tsum):
# write code here
if tsum < 3:
return []
small, big = 1, 2
mid = (1 + tsum) >> 1
curSum = small + big
result = []
while small < mid:
if curSum == tsum:
result.append(list(range(small, big + 1)))
while curSum > tsum and small < mid:
curSum -= small
small += 1
if curSum == tsum:
result.append(list(range(small, big + 1)))
big += 1
curSum += big
return result