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