python - 基于递归的python排列算法
问题描述
我尝试实现置换算法。它运作良好。
但是我关于这个算法中的递归的问题
置换函数:
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
解决方案
我明白了。基于堆栈的递归。这就是为什么它在 return 语句之后继续执行。
推荐阅读
- java - 在 Java 中使用 %n 或 \n 格式化字符串
- arrays - 如何从go中的C函数中获取out参数?
- javascript - Vimeo 视频无法在 iframe 中的 chrome 上启动。未调用 LoadProgress
- android - android中的Mapbox航点到达事件
- mongodb - mongodb从另一个json对象中的json对象搜索
- html - 包含背景大小不起作用
- php - Apache httpd 在 linux 服务器上挂起
- angular - 将多个项目(在 CLI 工作区中)延迟加载到一个大型应用程序
- javascript - 在编写 Photoshop 脚本时打开具有 JPG 扩展名的文件夹中的所有文件
- php - php中的扩展不起作用