python dfs


  • 0
    G
    class Solution(object):
        def dfs(self,res,tmp,pos,nums):
            res.append(tmp[:])
            for i in range(pos+1,len(nums)):
                tmp.append(nums[i])
                self.dfs(res,tmp,i,nums)
                tmp.pop()
        def subsets(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            if not nums:
                return [[]]
            res,tmp,pos=[],[],-1
            self.dfs(res,tmp,pos,nums)
            return res
    

  • 0
    S

    @goodbetterbest said in python dfs:

    res.append(tmp[:])

    Hi @goodbetterbest why do we need to append tmp like this: res.append(tmp[:])? why is tmp a empty list when appended to result like this: res.append(tmp)?

    Thanks.


Log in to reply
 

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