We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 2c4d645 + 57b99ef commit 01f792fCopy full SHA for 01f792f
ehco/recursion/recursion.py
@@ -0,0 +1,44 @@
1
+from collections import deque
2
+
3
4
+class Stack:
5
+ def __init__(self):
6
+ self._deque = deque()
7
8
+ def push(self, value):
9
+ self._deque.append(value)
10
11
+ def pop(self):
12
+ return self._deque.pop()
13
14
+ def is_empty(self):
15
+ return len(self._deque) == 0
16
17
18
+def print_num_use_stack(n):
19
+ s = Stack()
20
+ while n > 0:
21
+ s.push(n)
22
+ n -= 1
23
+ while not s.is_empty():
24
+ print(s.pop())
25
26
27
+def hanoi(n, a, b, c):
28
+ '''
29
+ 汉诺塔递归解决方法
30
31
+ 假设有A、B、C三个塔,
32
+ A塔有N块盘,目标是把这些盘全部移到C塔。
33
+ 那么先把A塔顶部的N-1块盘移动到B塔
34
+ 再把A塔剩下的大盘移到C
35
+ 最后把B塔的N-1块盘移到C。
36
37
+ 每次移动多于一块盘时,则再次使用上述算法来移动。
38
39
+ if n == 1:
40
+ print(a, '-->', c)
41
+ else:
42
+ hanoi(n - 1, a, c, b)
43
+ hanoi(1, a, b, c)
44
+ hanoi(n - 1, b, a, c)
0 commit comments