题目
描述
给出一组可能包含重复项的数字,返回该组数字的所有排列。结果以字典序升序排列。
数据范围: 0<n≤8 ,数组中的值满足 −1≤val≤5
要求:空间复杂度 O(n!),时间复杂度 O(n!)
思路
全排列问题,用深度优先搜素就可以了。
代码
python版本:
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param num int整型一维数组
# @return int整型二维数组
#
class Solution:
def permuteUnique(self , num: List[int]) -> List[List[int]]:
# write code here
res = []
cur = []
store = num
def dfs(cur: List[int], store: List[int]):
nonlocal res
if(len(store)==0):
if cur not in res:
res.append(cur)
for i in range(len(store)):
dfs(cur+[store[i]], store[:i]+store[i+1:])
dfs(cur, store)
def fun(x):
x = [str(v) for v in x]
str_x = "".join(x)
return str_x
res.sort(key=fun)
return res
c++版本:
无