leetcode78. 子集

17 篇文章 0 订阅
16 篇文章 0 订阅

就是组合算法,详细看我的另一篇非递归组合算法

class Solution(object):
    def combination(self,num_list, n):
        length = len(num_list)
        init_list = [1] * n + [0] * (length - n)
        start = True
        result = []
        while start:
            start = False
            judge = True
            if init_list[0] == 1:
                count=1
                tmp = [num_list[0]]
            else:
                count=0
                tmp = []
            for i in xrange(1, length):
                if init_list[i] == 1:
                    count+=1
                    tmp.append(num_list[i])
                if judge and init_list[i - 1] == 1 and init_list[i] == 0:
                    start = True
                    count-=1
                    init_list[i], init_list[i - 1] = init_list[i - 1], init_list[i]
                    for j in xrange(i-1):
                        if j<count:
                            init_list[j]=1
                        else:
                            init_list[j]=0
                    judge = False
            result.append(tmp)
        return result

    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        for i in xrange(len(nums)+1):
            result+=self.combination(nums,i)
        return result

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值