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
相关推荐
还有多久拿退休金42 分钟前
让飞书知识库跟着 commit 自己长:一套自动化知识库的真实实现
前端·算法·架构
KaMeidebaby2 小时前
卡梅德生物技术快报|小 RNA 适配体合成 + 多方法亲和力表征全流程标准化操作手册
前端·网络·数据库·人工智能·算法
是Dream呀2 小时前
基于深度学习的人类行为识别算法研究
人工智能·深度学习·算法
happyprince3 小时前
03_NVIDIA_ModelOpt-量化算法深入
人工智能·深度学习·算法
大鱼>3 小时前
AI+货物追踪:智能快递柜追踪系统
人工智能·深度学习·算法·机器学习
researcher-Jiang4 小时前
算法训练:堆 & 可并堆
算法
在书中成长4 小时前
HarmonyOS 小游戏《对战五子棋》开发第18篇 - 棋盘设计
算法·harmonyos
Frostnova丶4 小时前
(12)LeetCode 76. 最小覆盖子串
算法·leetcode·职场和发展
灯澜忆梦4 小时前
GO_函数_1
算法
旧曲重听14 小时前
为什么现在 RAG 越少越少提及了
数据库·程序人生·职场和发展·agent