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

题意:枚举所有子集。

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

代码:

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

 

相关文章:

  • 2022-03-06
  • 2021-10-02
  • 2021-12-10
  • 2022-01-30
  • 2021-12-30
  • 2022-12-23
猜你喜欢
  • 2022-02-26
  • 2022-12-23
  • 2022-02-27
  • 2021-11-29
  • 2022-01-25
  • 2021-11-09
  • 2021-08-09
相关资源
相似解决方案