Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums =
If nums =
[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ res = [] output = [] self.helper(nums, 0, res, output) return res def helper(self, nums, pos, res, output): if pos == len(nums): res.append(list(output)) else: self.helper(nums, pos+1, res, output) output.append(nums[pos]) self.helper(nums, pos+1, res, output) output.pop() |
但我感觉我需要再仔细想想为什么最后一步要pop,为什么append里面需要deepcopy of output list...
No comments:
Post a Comment