题解 | #汉诺塔问题#

汉诺塔问题

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

import java.util.*;

public class Solution {
    
    ArrayList<String> res = new ArrayList<>();

    public ArrayList<String> getSolution(int n) {
        // write code here
        process(n, "left", "mid", "right");
        return res;
    }

    public void process(int n, String x, String y, String z) {
        if (n == 1) {
            res.add("move from " + x + " to " + z);
        } else {
            process(n - 1, x, z, y);
            res.add("move from " + x + " to " + z);
            process(n - 1, y, x, z);
        }
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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