其他分享
首页 > 其他分享> > LC90. 子集 II

LC90. 子集 II

作者:互联网

同层剪枝法

class Solution(object):
    def subsetsWithDup(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        nums.sort()
        res = []
        sol = []
        def backtrack(res, sol, begin):
            res.append(sol[:])
            for i in range(begin, len(nums)):
                if i > begin and nums[i-1] == nums[i]:
                    continue
                sol.append(nums[i])
                backtrack(res, sol ,i+1)
                sol.pop()
        backtrack(res, sol, 0)
        return res
used数组法
class Solution(object):
    def subsetsWithDup(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        nums.sort()
        res = []
        sol = []
        vsd = [0]*len(nums)
        def backtrack(res, sol, vsd, begin):
            res.append(sol[:])
            for i in range(begin, len(nums)):
                if  i > 0 and nums[i] == nums[i-1] and vsd[i-1] == 0:
                    continue
                vsd[i] = 1
                sol.append(nums[i])
                backtrack(res, sol, vsd, i+1)
                vsd[i] = 0
                sol.pop()
        backtrack(res, sol, vsd, 0)
        return res  

标签:II,nums,res,backtrack,List,sol,LC90,子集,vsd
来源: https://blog.csdn.net/weixin_44392348/article/details/121322512