题目地址:https://leetcode.com/problems/subsets/description/

题目描述

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

For example,

If nums = [1,2,3], a solution is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

题目大意

给了输入数组,返回这个数组的所有能够成的子集。

解题方法

递归

先要找出一个数组全部的子集,可以从题目给的实例中找出答案。我们发现这个实例就是从数组中的每个数字都有不用两个状态。

因此可以写两个递归,分别表示用和不用当前元素的情况下,后续的递归结果。当index到达数组结尾的时候,就把路径path放入结果中就好了。

C++代码如下:

class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
dfs(res, {}, nums, 0);
return res;
}
void dfs(vector<vector<int>>& res, vector<int> path, vector<int>& nums, int index) {
if (index >= nums.size()) {
res.push_back(path);
return;
}
dfs(res, path, nums, index + 1);
path.push_back(nums[index]);
dfs(res, path, nums, index + 1);
}
};

上面的递归解法是当前元素用或者不用两种状态,另外一种递归解法是当前的元素一定使用,然后递归把后面的元素是否选择一些放入当前元素后面。

递归最重要的是明白递归函数的意义。下面代码的dfs()函数,就是在当前index元素使用的情况下,从nums的index后面抽取0个或者全部数字放入path的后面,注意这个for循环,意义是当前元素如果使用,后面的那个元素从哪里开始,也就决定了后面的数字选择多少个。

python代码如下:

class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
self.dfs(nums, 0, res, [])
return res def dfs(self, nums, index, res, path):
res.append(path)
for i in xrange(index, len(nums)):
self.dfs(nums, i + 1, res, path + [nums[i]])

回溯法

C++解法,同样的也是回溯法。

和上面的python递归解法有点类似,不过称其为回溯法是因为path是传的引用,这样path需要我们自己维护,添加元素弹出元素操作分别在递归函数的前后执行,从而达到了当前元素使用完成之后,进行回溯的效果。

C++代码如下:

class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
vector<int> path;
helper(nums, res, path, 0);
return res;
}
void helper(const vector<int>& nums, vector<vector<int>>& res, vector<int>& path, int start) {
res.push_back(path);
for (int i = start; i < nums.size(); i ++) {
path.push_back(nums[i]);
helper(nums, res, path, i + 1);
path.pop_back();
}
}
};

日期

2018 年 2 月 24 日
2018 年 12 月 20 日 —— 感冒害的我睡不着
2019 年 9 月 25 日 —— 做梦都在秋招,这个秋天有毒

【LeetCode】78. Subsets 解题报告(Python & C++)的更多相关文章

  1. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  2. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  5. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  6. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  7. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  8. 【LeetCode】916. Word Subsets 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...

  9. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

随机推荐

  1. 微信第三方平台获取component_verify_ticket

    官方文档说明: 在公众号第三方平台创建审核通过后,微信服务器会向其"授权事件接收URL"每隔10分钟定时推送component_verify_ticket.第三方平台方在收到tic ...

  2. CMSIS-RTOS 信号量Semaphores

    信号量Semaphores 和信号类似,信号量也是一种同步多个线程的方式,简单来讲,信号量就是装有一些令牌的容器.当一个线程在执行过程中,就可能遇到一个系统调用来获取信号量令牌,如果这个信号量包含多个 ...

  3. 求解线性递推方程第n项的一般方法

    概述 系数为常数,递推项系数均为一次的,形如下面形式的递推式,称为线性递推方程. \[f[n]=\begin{cases} C &n\in Value\\ a_1 f[n-1]+a_2 f[n ...

  4. 7 — 简单了解springboot中的thymeleaf

    1.官网学习地址 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html 2.什么是thymeleaf? 一张图看明白: 解读: ...

  5. 日常Java 2021/10/3

    方法: 用System.out.println()来解释,println()是一个方法,System是系统类,out 是标准输出对象. 也就是调用系统类中的对象中的方法. 注重方法:可以是程序简洁,有 ...

  6. keybd_event模拟键盘按键,mouse_event怎么用

    从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI: * Moving the mouse and clicki ...

  7. ace

    ace An ace is a playing card, die or domino with a single pip. In the standard French deck, an ace h ...

  8. React 16.13.1触发两次render

    一段很普通的代码,出发了两次render import React, { useState, useEffect } from 'react' const MouseTracker: React.FC ...

  9. SQL count和sum

    count(1).count(*)与count(列名)的执行区别 count(1) and count(字段) 两者的主要区别是 (1) count(1) 会统计表中的所有的记录数,包含字段为null ...

  10. error信息

    /opt/hadoop/src/contrib/eclipse-plugin/build.xml:61: warning: 'includeantruntime' was not set, defau ...