题解 | 有重复项数字的全排列

有重复项数字的全排列

https://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param num int整型一维数组 
# @return int整型二维数组
#
class Solution:
    def recursion(self,res:List[list[int]],num:List[int],index:int):
        if index == len(num)-1:
            if num.copy() not in res:#去重
                res.append(num.copy())
        else:
            for i in range(index,len(num)):
                #进行交互位置,改变排序,出现一种排序情况
                temp = num[i]
                num[i]=num[index]
                num[index] = temp
                #递归,下一位置
                self.recursion(res,num,index+1)

                #回溯
                temp = num[i]
                num[i]=num[index]
                num[index] = temp
            
    def permuteUnique(self , num: List[int]) -> List[List[int]]:
        # write code here
        num.sort()
        res = list(list())
        self.recursion(res,num,0)
        res.sort()
        return res

全部评论

相关推荐

求求给个offer我...:有这60不如v我50
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务