题解 | #汉诺塔问题#
汉诺塔问题
http://www.nowcoder.com/practice/7d6cab7d435048c4b05251bf44e9f185
class Solution {
public:
vector<string> solution;
void dfs(int n, string left, string mid, string right) {
if(n == 0)
return;
dfs(n - 1, left, right, mid);
solution.push_back("move from " + left + " to " + right);
dfs(n - 1, mid, left, right);
}
vector<string> getSolution(int n) {
// write code here
dfs(n, "left", "mid", "right");
return solution;
}
};