十二. 递归

面试题 08.06. 汉诺塔问题

class Solution:
    def move(self, n: int, A: List[int], B: List[int], C: List[int]) -> None:
        if n == 1:
            C.append(A.pop())
        else:
            self.move(n-1, A, C, B) # 将n-1个盘从A->B,借助C
            C.append(A.pop())
            self.move(n-1, B, A, C) # 将n-1个盘从B->C,借助A
    def hanota(self, A: List[int], B: List[int], C: List[int]) -> None:
        """
        Do not return anything, modify C in-place instead.
        """
        n = len(A)
        self.move(n, A, B, C)

剑指 Offer 64. 求1+2+…+n

class Solution:
    def sumNums(self, n: int) -> int:
        return 0 if n == 0 else n &

你可能感兴趣的:(算法,链表,leetcode,散列表)