题解 | #汉诺塔问题#
汉诺塔问题
https://www.nowcoder.com/practice/7d6cab7d435048c4b05251bf44e9f185
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return string字符串vector */ vector<string> res; vector<string> getSolution(int n) { f(n, "left", "mid", "right"); return res; } void f(int n, string x, string y, string z){ if(n == 0) return; f(n - 1, x, z, y); res.push_back("move from " + x + " to " + z); f(n - 1, y, x, z); } };