python leetcode 78. Subsets 90. Subsets II

78. Subsets

class Solution:
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        res=[[]]
        for n in nums:
            for i in range(len(res)):
                res.append([n]+res[i])
        return res

90. Subsets II

class Solution:
    def subsetsWithDup(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        res=[[]]
        nums.sort()
        for n in nums:
            for i in range(len(res)):
                val=[n]+res[i]
                if not val in res: res.append(val)
        return res

你可能感兴趣的:(leetcode,python,python,leetcode)