请实现一个函数打印最优移动轨迹。
给定一个 `int n` ,表示有 n 个圆盘。请返回一个 `string` 数组,其中的元素依次为每次移动的描述。描述格式为: `move from [left/mid/right] to [left/mid/right]`。
数据范围:
要求:时间复杂度
, 空间复杂度 )
2
["move from left to mid","move from left to right","move from mid to right"]
# 简简单单十几行 class Solution: def getSolution(self , n: int) -> List[str]: # write code here def hannuota(n, start: str, end: str): solution = [] all = ['left', 'mid', 'right'] all.remove(start) all.remove(end) midd = all[0] if n == 1: solution.append(f"move from {start} to {end}") return solution else: solution.extend(hannuota(n-1, start, midd)) solution.append(f"move from {start} to {end}") solution.extend(hannuota(n-1, midd, end)) return solution return hannuota(n, 'left', 'right')
class Solution: def getSolution(self , n: int) -> List[str]: # write code here def move(n,left,mid,right,res): if n==0: return move(n-1,left,right,mid,res) res.append('move from '+left+' to '+right) move(n-1,mid,left,right,res) return res res = [] return move(n,'left','mid','right',res)