[leetcode]Subsets @ Python

时间:2014-05-30 04:31:09   收藏:0   阅读:1243

原题地址:https://oj.leetcode.com/problems/subsets/

题意:枚举所有子集。

解题思路:碰到这种问题,一律dfs。

代码:

bubuko.com,布布扣
class Solution:
    # @param S, a list of integer
    # @return a list of lists of integer
    
    def subsets(self, S):
        def dfs(depth, start, valuelist):
            res.append(valuelist)
            if depth == len(S): return
            for i in range(start, len(S)):
                dfs(depth+1, i+1, valuelist+[S[i]])
        S.sort()
        res = []
        dfs(0, 0, [])
        return res
bubuko.com,布布扣

 

[leetcode]Subsets @ Python,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!