题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) O(n2)
blablabla
时间复杂度分析:blablabla
P 代码
class Solution:
def permutation(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return []
if len(nums) == 0:
return [[]]
self.res = []
self.dfs(sorted(nums), [])
return self.res
def dfs(self, nums, tmp):
if len(nums) == 0:
self.res += [tmp]
return
else:
for i in range(len(nums)):
if i > 0 and nums[i] == nums[i - 1]:
continue
self.dfs(nums[:i] + nums[i + 1:], tmp + [nums[i]])
import itertools class Solution: def permutation(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ per_0 = list(itertools.permutations(nums)) per = set([p for p in per_0]) # 这里取了个巧,没把数字序列转换成list,直接用tuple了 ans = list(per) return ans
解析:先用
itertools
库的permutations
函数生成输入数字的全排列(包含重复项),再用set
去重,最后把set
转换为list
即可