卖萌的弱渣

I am stupid, I am hungry.

Subsets

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

Note:

The solution set must not contain duplicate subsets.

Example:

If nums = [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

(Subsets.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution(object):
    def find_sets(self,nums,ret,tmp,index,n):
        if len(tmp) == n:
            ret.append(tmp[:])
            return
        for i in range(index,len(nums)):
            tmp.append(nums[i])
            self.find_sets(nums,ret,tmp,i+1,n)
            tmp.pop()
        return

    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        ret = []
        if not nums:
            return ret
        ret.append([])
        nums = sorted(nums)
        for i in range(1,len(nums)+1):
            self.find_sets(nums,ret,[],0,i)
        return ret