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
相关推荐
土司大王3 分钟前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
大熊背1 小时前
树莓派IspPipeline LSC模块原理详解
算法·lsc·isppipeline·mesh lsc
测试19981 小时前
Selenium 无法定位元素的几种解决方案
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
zander2582 小时前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
欧叶冲冲冲2 小时前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
祖力552 小时前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io
名字还没想好☜2 小时前
Go 用 slices/maps 标准库泛型函数:告别手写 Contains、Sort、去重(Go 1.21)
开发语言·后端·算法·golang·go
地平线开发者2 小时前
【模型轻量化专题】深度学习模型为什么需要轻量化
算法
圣保罗的大教堂2 小时前
leetcode 3622. 判断整除性 简单
leetcode
_Narcissus_3 小时前
链表算法题和静态链表
数据结构·c++·笔记·算法·链表·ai·力扣