卖萌的弱渣

I am stupid, I am hungry.

Subsets

Example

If S = [1,2,3], a solution is:

1
2
3
4
5
6
7
8
9
10
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Solution

  • DFS
(Subsets.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ass Solution:
    """
    @param S: The set of numbers.
    @return: A list of lists. See example.
    """
    def subset_helper(self,result,solution,nums):
        result.append(solution[:])

        for i,el in enumerate(nums):
            solution.append(el)
            self.subset_helper(result,solution,nums[i+1:])
            solution.pop()
                                                                                return

                                                                                def subsets(self, S):
        # write your code here
                                                                                    result = []
                                                                                    self.subset_helper(result,[],sorted(S))
                                                                                    return result