forked from cls1991/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path78_Subsets.py
49 lines (37 loc) · 931 Bytes
/
78_Subsets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# coding: utf8
"""
题目链接: https://leetcode.com/problems/subsets/description.
题目描述:
Given a set of distinct integers, nums, return all possible subsets (the power set).
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],
[]
]
"""
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return []
ans = []
sub = []
self.backtrace(nums, 0, sub, ans)
return ans
def backtrace(self, nums, idx, sub, ans):
ans.append(sub[:])
for i in range(idx, len(nums)):
sub.append(nums[i])
self.backtrace(nums, i + 1, sub, ans)
sub.pop()