Share my accepted simple python code

  • 6
    class Solution:
    # @param S, a list of integer
    # @return a list of lists of integer
    def subsets(self, S):
        if not S:
            return [[]]
            pre_subsets = self.subsets(S[1:])
            return pre_subsets + [[S[0]]+elem for elem in pre_subsets]

  • 0
    def subsets(self, S):
        def bt(i, n, S, cur): 
            if i == n: 
                return [cur] 
            return bt(i+1, n, S, cur) + bt(i+1, n, S, cur + [S[i]]) 
        return bt(0, len(S), sorted(S), [])

    Share a solution, which there's minor difference.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.