-
Notifications
You must be signed in to change notification settings - Fork 423
/
subsets.py
54 lines (39 loc) · 995 Bytes
/
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
50
51
52
53
54
"""
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
直接上递归,每条线都有两个决策:
1. 加上。
2. 不加。
beat 96%
测试地址:
https://leetcode.com/problems/subsets/description/
"""
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
length = len(nums)
def makeSubsets(index, current_subsets):
if index == length:
return
result.append(current_subsets+[nums[index]])
makeSubsets(index+1, current_subsets+[nums[index]])
makeSubsets(index+1, current_subsets)
makeSubsets(0, [])
return result+[[]]