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], [] ]
class Solution:
# @param num, a list of integer
# @return a list of lists of integer
def subsetsWithDup(self, S):
if len(S)==0:
return [[]]
res = [[]]
S = sorted(S)
count = 1
for ind,elem in enumerate(S):
if ind==0:
start = 0;
elif S[ind-1] == S[ind]:
count = count + 1
start = len(res)*(count-1)/count
else:
start,count = 0,1
newres = []
for index in range(start,len(res)):
elemres = res[index]
new = elemres[:]
new.append(elem)
newres.append(new)
res = res + newres
return res
not too hard