题解 | #汉诺塔问题#

汉诺塔问题

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

递归脑壳疼

class Solution {
public:
  
    void hanoi(std::vector<std::string> &description, int n, 
               std::string left, std::string mid, std::string right) {
      if (n == 1) {
        description.emplace_back("move from " + left + " to " + right);
        return ;
      }
      
      hanoi(description, n-1, left, right, mid);
      description.emplace_back("move from " + left + " to " + right);
      hanoi(description, n-1, mid, left, right);
      
    }
  
    vector<string> getSolution(int n) {
      std::vector<std::string> description;
      hanoi(description, n, "left", "mid", "right");
      return description;
    }
};
全部评论

相关推荐

03-05 12:52
吉林大学 Java
挣K存W养DOG:他的价值在于把他家里积攒的财富回馈给社会
点赞 评论 收藏
分享
刘湘_passion:出国旅游?那就小心你的腰子咯
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务