Study/Algorithm

[leetcode]78. Subsets

DoRoMii 2019. 8. 10. 10:54
728x90

78. Subsets

 

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3] Output: [ [3],   [1],   [2],   [1,2,3],   [1,3],   [2,3],   [1,2],   [] ]

 

class Solution(object):
    def subsets(self, nums):
        index = 0
        curs = [[]]
        len_nums = len(nums)
        
        while index < len_nums:
            next_step =[]
            for cur in curs:
                next_step.append([i for i in cur])
                next_step.append([i for i in cur]+[nums[index]])
            index +=1
            curs = next_step
        return next_step
                

https://leetcode.com/problems/subsets/