Subsets
Problem Link: https://leetcode.com/problems/subsets/
Choose or Don't choose Method
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
Faith Method:
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
if(len(nums)==1):
return [[],[nums[0]]]
tempAns=self.subsets(nums[1:])
n=len(tempAns)
ans=tempAns
for i in range(n):
temp=tempAns[i][:]
# Take care of the above line [:]
# Create another copy of that
temp.insert(0,nums[0])
ans.append(temp)
return ans
Last updated