栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

leetcode 47. 全排列 II python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode 47. 全排列 II python

题目描述:

题解一:

与46全排列相同,只是当depth=n,在path加入res之前进行一步判断,如果当前得到的path在res中不存在则添加。 

class Solution(object):
    def permuteUnique(self, nums):
        n = len(nums)
        used = [0 for i in range(n)]
        depth = 0
        res = []
        path = []
        def dfs(nums,n,depth,used,res,path):
            if depth==n:
                newpath = path[:]
                if newpath not in res:
                    res.append(path)
                return
            for i in range(n):
                if used[i]==0:
                    used[i]=1
                    path.append(nums[i])
                    dfs(nums,n,depth+1,used,res,path)
                    path.pop()
                    used[i] = 0
        dfs(nums,n,depth,used,res,path)
        return res

方法二:同一层不能选相同的数字

参考LeetCode 47. 全排列 II | Python_"大梦三千秋的博客-CSDN博客

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323945.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号