Subsets

Question

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

Thoughts

输出的subset要有序,小的value不能出现在大的element后面

Solution

class Solution:
    """
    @param S: The set of numbers.
    @return: A list of lists. See example.
    """
    def subsets(self, S):
        # write your code here
        self.S = S
        self.S.sort()
        self.resultList = []
        self.subsetHelper([], 0)
        return self.resultList

    def subsetHelper(self, curList, pos):
        newList = list(curList)
        self.resultList.append(newList)

        for i in range(pos, len(self.S)):
            curList.append(self.S[i])
            self.subsetHelper(curList, i + 1)
            curList.pop()