题解 | #汉诺塔问题#
汉诺塔问题
https://www.nowcoder.com/practice/7d6cab7d435048c4b05251bf44e9f185
package main
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return string字符串一维数组
*/
func getSolution( n int ) []string {
// write code here
datas := make([]string,0)
var f func(c int,from,mid,end string)
f = func(c int,from,mid,end string){
if c == 1{
datas = append(datas,"move from "+from+" to "+end)
return
}
f(c-1,from,end,mid)
datas = append(datas,"move from "+from+" to "+end)
f(c-1,mid,from,end)
}
f(n,"left","mid","right")
return datas
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return string字符串一维数组
*/
func getSolution( n int ) []string {
// write code here
datas := make([]string,0)
var f func(c int,from,mid,end string)
f = func(c int,from,mid,end string){
if c == 1{
datas = append(datas,"move from "+from+" to "+end)
return
}
f(c-1,from,end,mid)
datas = append(datas,"move from "+from+" to "+end)
f(c-1,mid,from,end)
}
f(n,"left","mid","right")
return datas
}

