바위타는 두루미

[leetcode]78. Subsets 본문

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/

'Study > Algorithm' 카테고리의 다른 글

[leetcode]75. Sort Colors  (0) 2019.08.10
[leetcode]79. Word Search  (0) 2019.08.10
[leetcode]46. Permutations  (0) 2019.08.10
[leetcode]22. Generate Parentheses  (0) 2019.08.09
[leetcode]17. Letter Combinations of a Phone Number  (0) 2019.08.09
Comments