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
相关推荐
NEFU_Jyj1 小时前
牛客周赛R168
算法
文丑颜不良啊8 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z9 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH9 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶10 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊10 小时前
LeetCode2161~2180题目解析
java·leetcode
雪落漂泊10 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
文丑颜不良啊12 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发12 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview