给定一个可包含重复数字的序列 nums
,按任意顺序 返回所有不重复的全排列。
示例 1:
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]
示例 2:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
提示:
1 <= nums.length <= 8
-10 <= nums[i] <= 10
递归求解:
# [46] 全排列
#
# @lc code=start
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
import copy
def subpermute(nums,index):
if index==0:
return [[nums[0]]]
else:
rev=subpermute(nums,index-1)
res=list()
for i in range(len(rev)):
#print(rev[i])
for j in range(len(rev[i])+1):
if j==len(rev[i]) or rev[i][j]!=nums[index]:
mid=copy.deepcopy(rev[i])
#print(mid)
mid.insert(j,nums[index])
if mid not in res:
res.append(mid)
return res
index=len(nums)-1
return subpermute(nums,index)