moonlightshadow123
6/24/2017 - 11:21 AM

78. Subsets

  1. Subsets
class Solution(object):
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        self.nums = sorted(nums)
        self.res = []
        self.cur = []
        self.backtracking(0)
        return self.res
    def backtracking(self, start):
        self.res.append([each for each in self.cur])
        if len(self.nums)-1 < start:
            return
        for i in range(start, len(self.nums)):
            self.cur.append(self.nums[i])
            self.backtracking(i+1)
            del self.cur[-1]

https://leetcode.com/problems/subsets/#/description

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

Note: The solution set must not contain duplicate subsets.

For example, If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]