题解 | #汉诺塔问题 回溯#
汉诺塔问题
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);
}
}
}