题解 | #汉诺塔问题 回溯#

汉诺塔问题

http://www.nowcoder.com/practice/7d6cab7d435048c4b05251bf44e9f185

import java.util.*;

public class Solution {
    static ArrayList<String> list = new ArrayList<String>();
    public ArrayList<String> getSolution(int n) {
        // write code here
        hanoi(n, "left","mid","right");
        return list;
    }
    public static void hanoi(int n, String left, String mid, String right){
        if (n == 1){
            list.add("move from " + left + " to " + right);
        }else {
            hanoi(n - 1,left, right, mid);
            list.add("move from " + left + " to " + right);
            hanoi(n - 1, mid, left, right);
        }
    }
}
全部评论

相关推荐

死在JAVA的王小美:哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈,我也是,让我免了一轮,但是硬气拒绝了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务