LeetCode讲解篇之78. 子集

文章目录

题目描述

题解思路

初始化一个start变量记录当前从哪里开始遍历搜索nums

搜索过程的数字组合加入结果集

然后从start下标开始遍历nums,更新start,递归搜索

直到搜索完毕,返回结果集

题解代码

python 复制代码
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        res = []
        tmp = []
        n = len(nums)
        start = 0
        def dfs():
            nonlocal start
            res.append([num for num in tmp])
            for i in range(start, n):
                tmp.append(nums[i])
                start = i + 1
                dfs()
                tmp.pop()

        dfs()
        return res
相关推荐
ysu_031421 分钟前
05 | 持久化撤销提示非核心功能
算法·游戏程序
浮沉9872 小时前
二分查找算法概述&通用模板
算法
Keven_113 小时前
算法札记:SPFA判负环算法的证明
算法
什巳3 小时前
JAVA练习278- 和为 K 的子数组
java·学习·算法·leetcode
Jerry3 小时前
LeetCode 347. 前 K 个高频元素
算法
Young Doro3 小时前
SAC 算法
线性代数·算法·机器学习
罗超驿4 小时前
2.算法效率的核心密码:时间复杂度和空间复杂度详解
java·数据结构·算法
:-)4 小时前
算法-堆排序
数据结构·算法·排序算法