46. Permutations 排列
阿新 • • 發佈:2018-02-06
for num win spa www ssi collect return bsp
來自為知筆記(Wiz)
Given a collection of distinct numbers, return all possible permutations.
For example,[1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
class Solution:
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
s = set(nums)
def gen(l, added):
if len(l) is len(nums):
res.append(l[:])
else:
for i in s:
if not i in added:
l.append(i)
added.add(i)
gen
(l, added)l.pop()
added.remove(i)
gen([], set())
return res
來自為知筆記(Wiz)
46. Permutations 排列