forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubsets.py
144 lines (125 loc) · 3.04 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
from __future__ import division
import random
'''
Leetcode: Subsets
Given a set of distinct integers, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
'''
def subsets_generator(S):
if len(S) == 1: yield S
else:
for i in range(len(S)):
ch = S[i]
yield [ch]
if S[i+1:]:
for other in subsets_generator(S[i+1:]):
yield [ch] + other
def subsets(S):
print '\nSubsets of', S
S = sorted(S)
for sset in subsets_generator(S):
print sset
print []
def subsets_iter(S):
print '\nSubsets of', S
S = sorted(S)
ss = []
for ch in S:
if ss:
ss += [sset + [ch] for sset in ss]
else:
ss.append([])
ss.append([ch])
print ss
return ss
'''
Leetcode: Subsets ||
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
'''
def subsets2_generator(S):
if len(S) == 1: yield S
else:
checked = set()
for i in range(len(S)):
ch = S[i]
if ch in checked: continue
checked.add(ch)
yield [ch]
if S[i+1:]:
for other in subsets2_generator(S[i+1:]):
yield [ch] + other
def subsets2(S):
print '\nSubsets of', S
S = sorted(S)
for sset in subsets_generator2(S):
print sset
print []
# interesting solution!
def subsets2_iter(S):
print '\nSubsets of', S
S = sorted(S)
ss = [[]]
start = 0
for i in range(len(S)):
cur_size = len(ss)
for j in range(start, cur_size):
sub = list(ss[j])
sub.append(S[i])
ss.append(sub)
if i < len(S)-1 and S[i+1] == S[i]:
# if next character is duplicated
# only add it to the last few subarrays in the prev loop
start = cur_size
else:
start = 0
print ss
return ss
## Print out all the subsets of an array without storing any subset.
## Index all the elements, and print out subsets according to binary numbers.
def subset_binary(L):
n = len(L)
for b in range(2**n):
selected_indices = []
i = 0
while b > 0:
b, bit = divmod(b, 2)
if bit == 1: selected_indices.append(i)
i += 1
#print selected_indices
yield [L[k] for k in selected_indices]
if __name__ == '__main__':
'''
subsets_iter([1,2,3])
subsets_iter([10,1,4,3])
subsets2_iter([1,2,2,2])
subsets2_iter([1,1,1,1])
'''
for x in subset_binary([1,2,3,4]): print x