【发布时间】:2021-06-23 19:13:02
【问题描述】:
这是一个link to a youtube video @ 11:50,它显示了以下递归树:
代码类似于:
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = []
def helper(curr: List[int], remains: List[int], res: List[List[int]]):
if not remains:
res.append(curr)
return
for i in range(len(remains)):
next_num = remains[i]
helper(curr + [next_num], [num for num in remains if num != next_num], res)
helper([], nums, res)
return
视频说有 O(n!) 函数调用,但它似乎有点多所以可能大致是这样,但是在每一步中我们必须在最坏的情况下复制 n新剩余数组的元素,那为什么不变成 O(n * n!)
【问题讨论】:
标签: python algorithm permutation