The question is : https://leetcode.com/problems/subsets/
First code
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
ans=[]
def recursion(nums,tempAns):
if(len(nums)==0):
ans.append(tempAns)
return
temp1=tempAns[:]
temp2=temp1
temp2.append(nums[0])
recursion(nums[1:],temp1)
recursion(nums[1:],temp2)
recursion(nums,[])
return ans
Second Code
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
ans=[]
def recursion(nums,tempAns):
if(len(nums)==0):
ans.append(tempAns)
return
ch=nums[0]
restofarray=nums[1:]
temp=tempAns[:]
temp.append(ch)
recursion(restofarray,tempAns)
recursion(restofarray,temp)
recursion(nums,[])
return ans
While the second one works, first one shows wrong output. Can I know the reason please?