47. 全排列 II

给定一个可包含重复数字的序列 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

题出的跟白给一样,上一个写出来后稍加修改就行了

python 复制代码
class Solution:
    def __init__(self):
        self.recode=  {}

    def merge(self,lis,target):
        res = set()
        for i in lis:
            for j in range(len(i)+1):
                new_i = i[::]
                new_i.insert(j,target)
                res.add(tuple(new_i))
        return [list(i) for i in res]

        


    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        if len(nums) ==1:
            return [nums]
        res = []
        for ind,v in enumerate(nums[1:],1):
            key = tuple(nums[:ind])
            tp = self.recode.get(key)
            if not tp:
                tp = self.permuteUnique(nums[:ind])
            res = self.merge(tp,v)
        self.recode[tuple(nums)] = res
        return res
        
相关推荐
UrbanJazzerati1 天前
Vue3 父子组件通信完全指南
前端·面试
UrbanJazzerati1 天前
Vue 3 纯小白快速入门指南
前端·面试
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿1 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
NAGNIP2 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP2 天前
一文搞懂激活函数!
算法·面试
用户8356290780512 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
前端Hardy2 天前
面试官:JS数组的常用方法有哪些?这篇总结让你面试稳了!
javascript·面试
牛奶2 天前
React 底层原理 & 新特性
前端·react.js·面试
牛奶2 天前
ts随笔:面向对象与高级类型
前端·面试·typescript