Skip to content

Commit 3a3d883

Browse files
authored
Create tower_of_hanoi.py
1 parent 712485b commit 3a3d883

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tower_of_hanoi.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
"""
3+
@param n: the number of disks
4+
@return: the order of moves
5+
"""
6+
def towerOfHanoi(self, n):
7+
# write your code here
8+
ret = []
9+
if n > 0:
10+
Solution.move(n, 'A', 'C', 'B', ret)
11+
return ret
12+
13+
@staticmethod
14+
def move(n, source, target, by, ret):
15+
if n == 1:
16+
ret.append('from %s to %s' % (source, target))
17+
else:
18+
Solution.move(n - 1, source, by, target, ret)
19+
ret.append('from %s to %s' % (source, target))
20+
Solution.move(n - 1, by, target, source, ret)
21+
22+
# medium: http://lintcode.com/zh-cn/problem/tower-of-hanoi/

0 commit comments

Comments
 (0)