全排列 五种写法
def permutation(nums):
if not nums:
return []
if len(nums) == 1:
return [nums]
res, size = [], len(nums)
q = collections.deque()
q.append([])
while q:
n = q.pop()
if len(n) == size:
res.append(n[:])
continue
for x in nums:
if x not in n:
q.append(n+[x])
return res
#input: [1, 2, 3]
# output: [[3, 2, 1], [3, 1, 2], [2, 3, 1], [2, 1, 3], [1, 3, 2], [1, 2, 3]]
最后更新于