# 0078. 子集 ## 题目地址(78. 子集) <https://leetcode-cn.com/problems/subsets/> ## 题目描述 ``` <pre class="calibre18">``` 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] ``` ``` ## 前置知识 - 回溯 ## 公司 - 阿里 - 腾讯 - 百度 - 字节 ## 思路 这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。 这种题目其实有一个通用的解法,就是回溯法。 网上也有大神给出了这种回溯法解题的 [通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)>),这里的所有的解法使用通用方法解答。 除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。 我们先来看下通用解法的解题思路,我画了一张图: ![](https://img.kancloud.cn/46/ec/46ec3382e572355e2109c47284e37e4b_1341x1080.jpg) > 每一层灰色的部分,表示当前有哪些节点是可以选择的, 红色部分则是选择路径。1,2,3,4,5,6 则分别表示我们的 6 个子集。 通用写法的具体代码见下方代码区。 ## 关键点解析 - 回溯法 - backtrack 解题公式 ## 代码 - 语言支持:JS,C++ JavaScript Code: ``` <pre class="calibre18">``` <span class="hljs-title">/* * @lc app=leetcode id=78 lang=javascript * * [78] Subsets * * https://leetcode.com/problems/subsets/description/ * * algorithms * Medium (51.19%) * Total Accepted: 351.6K * Total Submissions: 674.8K * Testcase Example: '[1,2,3]' * * Given a set of distinct integers, nums, return all possible subsets (the * power set). * * Note: The solution set must not contain duplicate subsets. * * Example: * * * Input: nums = [1,2,3] * Output: * [ * ⁠ [3], * [1], * [2], * [1,2,3], * [1,3], * [2,3], * [1,2], * [] * ] * */</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">backtrack</span>(<span class="hljs-params">list, tempList, nums, start</span>) </span>{ list.push([...tempList]); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start; i < nums.length; i++) { tempList.push(nums[i]); backtrack(list, tempList, nums, i + <span class="hljs-params">1</span>); tempList.pop(); } } <span class="hljs-title">/** * @param {number[]} nums * @return {number[][]} */</span> <span class="hljs-keyword">var</span> subsets = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">nums</span>) </span>{ <span class="hljs-keyword">const</span> list = []; backtrack(list, [], nums, <span class="hljs-params">0</span>); <span class="hljs-keyword">return</span> list; }; ``` ``` C++ Code: ``` <pre class="calibre18">``` <span class="hljs-keyword">class</span> Solution { <span class="hljs-keyword">public</span>: <span class="hljs-params">vector</span><<span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>> subsets(<span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>& nums) { <span class="hljs-keyword">auto</span> ret = <span class="hljs-params">vector</span><<span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>>(); <span class="hljs-keyword">auto</span> tmp = <span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>(); backtrack(ret, tmp, nums, <span class="hljs-params">0</span>); <span class="hljs-keyword">return</span> ret; } <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">backtrack</span><span class="hljs-params">(<span class="hljs-params">vector</span><<span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>>& <span class="hljs-params">list</span>, <span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>& tempList, <span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>& nums, <span class="hljs-keyword">int</span> start)</span> </span>{ <span class="hljs-params">list</span>.push_back(tempList); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i = start; i < nums.size(); ++i) { tempList.push_back(nums[i]); backtrack(<span class="hljs-params">list</span>, tempList, nums, i + <span class="hljs-params">1</span>); tempList.pop_back(); } } }; ``` ``` ## 相关题目 - [39.combination-sum](39.combination-sum.html) - [40.combination-sum-ii](40.combination-sum-ii.html) - [46.permutations](46.permutations.html) - [47.permutations-ii](47.permutations-ii.html) - [90.subsets-ii](90.subsets-ii.html) - [113.path-sum-ii](113.path-sum-ii.html) - [131.palindrome-partitioning](131.palindrome-partitioning.html)