-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrecursion.py
More file actions
83 lines (69 loc) · 1.25 KB
/
recursion.py
File metadata and controls
83 lines (69 loc) · 1.25 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
python3.6.4
@Date : "2018-10-14"
@Author :"HerryZhang"
#Reference :https://time.geekbang.org/column/article/41440
"""
def seat(n):
"""
座位问题
"""
if n == 1:
return 1
return seat(n - 1) + 1
def seat_plus(n):
"""
座位问题
递归变成递推
"""
res = 1
for i in range(2, n + 1):
res = res + 1
return res
def walk(n):
"""
阶梯的走法
"""
if n == 1:
return 1
if n == 2:
return 2
return walk(n - 1) + walk((n - 2))
d = {}
def walk_plus(n):
"""
阶梯的走法
解决重复计算问题
"""
if n == 1:
return 1
if n == 2:
return 2
if d.get(n):
return d.get(n)
res = walk_plus(n - 1) + walk_plus((n - 2))
d[n] = res
return res
def walk_no_recurrence(n):
"""
阶梯的走法
递归变成递推
"""
if n == 1:
return 1
if n == 2:
return 2
ret = 0
pre = 2
prepre = 1
for i in range(3, n + 1):
ret = pre + prepre
prepre = pre
pre = ret
return ret
if __name__ == "__main__":
print(seat(10))
print(seat_plus(10))
print(walk(5))
print(walk_plus(5))
print(walk_no_recurrence(5))