题解 | #汉诺塔问题#

汉诺塔问题

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
    
}

  




全部评论

相关推荐

点赞 评论 收藏
分享
12-15 12:50
河北工程大学
sta666:我也是这个国际商业化的,三天,一天一面,就通过了,不过我是后端实习生,好好面感觉能过。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务