Easy Python Solution


  • 0
    M
    class Solution(object):
        def subsets(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            
            if nums==None:
                return []
            elif nums==[]:
                return [[]]
                
            res=[]
            
            self.helper(nums,[],res,0)
            return res
            
        def helper(self,nums,tmp,res,i):
            
            if i==len(nums):
                res.append(tmp[:])
                return
            
            self.helper(nums,tmp,res,i+1)
            tmp.append(nums[i])
            self.helper(nums,tmp,res,i+1)
            tmp.remove(tmp[-1])
    

  • 0
    M

    @mada0304 why do you append tmp[:] instead of tmp? I thought they were the same but append tmp does not work.


  • 0
    S

    if you use "tmp", every "tmp" from here will change. if you have time, you can look some about copy module and learn different copy type. You will understand why~


  • 0
    M

    @MaoDD tmp will be a pointer, and tmp[:] will be a copy.


Log in to reply
 

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