Subsets-Problem-on-Backtracking-Method - Yaqin Tang Subsets-Problem-on-Backtracking-Method | Yaqin Tang - Homepage

Subsets-Problem-on-Backtracking-Method

Given a set of distinct integers, nums, return all possible subsets.

class Solution(object):
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if len(nums) == 0:
            return [[]]
        result = []
        self.subset(result, [], 0, nums)
        return result

    def subset(self, result, subList, start, nums):
        if subList not in result:
            result.append(list(subList))
        for indx in xrange(start, len(nums)):
            subList.append(nums[indx])
            self.subset(result, subList, indx + 1, nums)
            subList.pop()