Skip to content

Latest commit

 

History

History

78-Subsets

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Subsets

Problem can be found in here!

Solution: Backtracking

def subsets(nums: List[int]) -> List[List[int]]:
    def helper(tempt: List[int], index: int):
        if len(tempt) <= len(nums):
            result.append(tempt)

        for i in range(index, len(nums)):
            helper(tempt+[nums[i]], i+1)

    result = []
    helper([], 0)
    return result

Explanation: We can apply depth-first search to solve this problem. The key to avoid duplicated solutions is to pass in a variable index in each iteration of dfs.

Time Complexity: O(n*2^n), Space Complexity: O(n), where n is the length of nums.