首页 > 技术文章 > 卧槽,用二进制,绝了

ihuhuhu 2018-09-19 19:38 原文

给定一个可能具有重复数字的列表,返回其所有可能的子集

 

from functools import reduce


class Solution:
    def subsetsWithDup(self, S):
        # write your code here
        S.sort()
        p = [[S[x] for x in range(len(S)) if i>>x&1] for i in range(2**len(S))]
        func = lambda x,y:x if y in x else x + [y]
        p = reduce(func, [[], ] + p)
        print(len(p))
        return list((p))

 

推荐阅读