打卡群刷题总结0724——子集

时间:2022-07-22
本文章向大家介绍打卡群刷题总结0724——子集,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目:78. 子集

链接:https://leetcode-cn.com/problems/subsets

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]

解题:

1、DFS回溯。

代码:

class Solution(object):
    def _subsets_helper(self, nums, start, current):
        for i in range(start, len(nums)):
            current2 = copy.copy(current)
            current2.append(nums[i])
            self.res.append(current2)
            self._subsets_helper(nums, i + 1, current2)

    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        self.res = [[]]
        self._subsets_helper(nums, 0, [])
        return self.res

PS:刷了打卡群的题,再刷另一道题,并且总结,确实耗费很多时间。如果时间不够,以后的更新会总结打卡群的题。

PPS:还是得日更呀,总结一下总是好的。