【问题标题】:python permutations algorithm based on recursion基于递归的python排列算法
【发布时间】:2021-12-01 06:55:45
【问题描述】:

我尝试实现置换算法。效果很好。

但是我对这个算法中递归的问题

置换函数:

def permutations(word):

return 语句后它如何以及为什么继续工作?

    if len(word) == 1:
        return [word]

   

如果 last perms == ['3'] 怎么回去 怎么从 ['3'] 回到 ['23']?

    perms = permutations(word[1:])
    char = word[0]
    result = []

    for perm in perms:
        for i in range(len(perm) + 1):
            result.append(perm[:i] + char + perm[i:])
    return result

【问题讨论】:

    标签: python recursion permutation


    【解决方案1】:

    我明白了。基于堆栈的递归。这就是为什么它在 return 语句之后继续执行。

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 2012-10-24
      • 2012-10-18
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多