leetcode78_ subset

Given a set of no repeating element integer array nums, which returns an array of all possible subsets (power set).

Description: Solution Set can not contain duplicate subsets.

Example:

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

This requires this specific path, the dynamic programming can not be used, only back
1, the current position as the Origin can jump down Mopai all positions
2, eventually return to the recursive Origin position
3, then the following position as a Origin position, repeat the above operation

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        result = []
        search = []
        self._dfs(nums, 0, search, result)
        return result
    
    def _dfs(self, nums, index, search, result):
        result.append(search.copy())
        for i in range(index, len(nums)):
            # 以当前位置为源流
            search.append(nums[i]) 
            # 往下摸排所有可以跳到的位置 
            self._dfs(nums, i + 1, search, result)
            # 最终递归返回源流位置,然后再以下面一个位置作为源流位置,重复上述操作
            search.pop()
Published 36 original articles · won praise 18 · views 6524

Guess you like

Origin blog.csdn.net/weixin_40027284/article/details/104820482