Leetcode算法题-递归

it2024-03-21  79

面试题 08.06. 汉诺塔问题

https://leetcode-cn.com/problems/hanota-lcci/

class Solution { public void hanota(List<Integer> A, List<Integer> B, List<Integer> C) { movePlat(A.size(), A, B, C); } public void movePlat(int sum, List<Integer> A, List<Integer> B, List<Integer> C) { if (sum == 1) C.add(A.remove(A.size() - 1)); else { movePlat(sum - 1, A, C, B); C.add(A.remove(A.size() - 1)); movePlat(sum - 1, B, A, C); } } }

 

最新回复(0)