forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsets.py
More file actions
35 lines (33 loc) · 1.1 KB
/
subsets.py
File metadata and controls
35 lines (33 loc) · 1.1 KB
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
# -*- coding: utf-8 -*-
class Solution:
"""
@param S: The set of numbers.
@return: A list of lists. See example.
"""
def subsets(self, S):
# write your code here
ret = []
S.sort()
for i in xrange(len(S) + 1):
combs = self._subsets(S, 0, i) # 计算包含i个元素的子集
for comb in combs:
ret.append(comb)
return ret
def _subsets(self, numbers, pos, k):
n = len(numbers) - pos
if (not numbers) or (n < k):
return None
ret = []
if k == 0: # 0/1特殊情况处理
return [[]]
for i in xrange(pos, len(numbers), 1):
combs = self._subsets(numbers, i + 1, k - 1) # 计算i + 1开始位置,包含k - 1个元素的子集
if combs:
if combs != [[]]:
for comb in combs:
new_comb = [numbers[i]]
new_comb.extend(comb)
ret.append(new_comb)
else:
ret.append([numbers[i]])
return ret