forked from Lazy-Pig/CodingInterviewChinese2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
57_和为s的数字.py
56 lines (39 loc) · 1.14 KB
/
57_和为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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
题目一: 和为s的两个数字
"""
def find_nums_with_sum(array, sum):
if not isinstance(array, list) or len(array) == 0 or not isinstance(sum, int):
return
left = 0
right = len(array) - 1
while left < right:
current_sum = array[left] + array[right]
if current_sum == sum:
return True, array[left], array[right]
if current_sum < sum:
left += 1
if current_sum > sum:
right -= 1
return False, None, None
"""
题目二: 和为s的连续正数序列
"""
def find_continuous_sequence(target):
if not isinstance(target, int) or target <= 0:
return
left = 1
right = 2
middle = (target + 1) // 2
while left < middle:
sequence = [x for x in range(left, right + 1)]
current_sum = sum(sequence)
if current_sum == target:
print(sequence)
right += 1
if current_sum > target:
left += 1
if current_sum < target:
right += 1
if __name__ == "__main__":
print(find_nums_with_sum([1, 2, 4, 7, 11, 15], 20))
find_continuous_sequence(15)