smj007

Untitled

Jun 21st, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. # First Implementation
  2. class Solution:
  3.     def subsets(self, nums: List[int]) -> List[List[int]]:
  4.        
  5.         def generate_subsets(start):
  6.            
  7.             if start >= len(nums):
  8.                 return [[]]
  9.            
  10.             subsets_without_num = generate_subsets(start + 1)
  11.             subsets_with_num = [[nums[start]] + subset for subset in subsets_without_num]
  12.            
  13.             return subsets_without_num + subsets_with_num
  14.        
  15.         return generate_subsets(0)
  16.  
  17. # Second Implementation
  18. class Solution:
  19.     def subsets(self, nums: List[int]) -> List[List[int]]:
  20.         if (len(nums)) == 0:
  21.             return [[]]
  22.        
  23.         start = 0
  24.         subsets_without_num = self.subsets(nums[:start] + nums[start+1:])
  25.         subsets_with_num = [[nums[start]] + subset for subset in subsets_without_num]
  26.        
  27.         return subsets_without_num + subsets_with_num
Advertisement
Add Comment
Please, Sign In to add comment