LeetCode Subsets (DFS)

题意:

  给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复)。

思路:

  DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计。

 1 class Solution {
 2 public:
 3     vector<vector<int>> subsets(vector<int>& nums) {
 4         sort(nums.begin(),nums.end());
 5         DFS(0,nums,tmp);
 6         return ans;
 7     }
 8
 9     void DFS(int pos,vector<int>& nums,vector<int>& seq)
10     {
11         ans.push_back(seq);
12         for( ; pos<nums.size(); pos++)
13         {
14             seq.push_back(nums[pos]);
15             DFS(pos+1,nums,seq);    //放
16             seq.pop_back();
17         }
18     }
19 private:
20     vector<vector<int>> ans;
21     vector<int> tmp;
22 };

AC代码

  迭代解决:由于集合中的元素是不可能出现相同的,所以子集的个数必定是2n个,即每个数字有可取可不取这两种选择。

 1 class Solution {
 2 public:
 3     vector<vector<int>> subsets(vector<int>& nums) {
 4         sort(nums.begin(),nums.end());
 5         int n=nums.size();
 6         for(int i=0; i<(1<<n); i++)
 7         {
 8             vector<int> tmp;
 9             for(int j=0; j<n; j++)
10             {
11                 if((i>>j)&1)
12                     tmp.push_back(nums[j]);
13             }
14             ans.push_back(tmp);
15         }
16         return ans;
17     }
18 private:
19     vector<vector<int>> ans;
20 };

AC代码

  如果集合中有相同的元素的话,解法同LEETCODE COMBINATION SUM II (DFS),主要在于去重,而去重的技术一样。

 1 class Solution {
 2 public:
 3     vector<vector<int>> subsets(vector<int>& nums) {
 4         sort(nums.begin(),nums.end());
 5         DFS(0,nums,tmp);
 6         ans.push_back(vector<int>());
 7         return ans;
 8     }
 9
10     void DFS(int pos,vector<int>& nums,vector<int>& seq)
11     {
12         if(pos>=nums.size())
13         {
14             if(!seq.empty())    ans.push_back(seq);
15             return ;
16         }
17         for( ; pos<nums.size(); pos++)
18         {
19             seq.push_back(nums[pos]);
20             DFS(pos+1,nums,seq);    //放
21             seq.pop_back();
22
23             while(pos+1<nums.size() && nums[pos]==nums[pos+1])    pos++;//主要在这
24         }
25         DFS(pos+1,nums,seq);
26     }
27 private:
28     vector<vector<int>> ans;
29     vector<int> tmp;
30 };

AC代码

时间: 2024-08-02 02:48:46

LeetCode Subsets (DFS)的相关文章

LeetCode Combinations (DFS)

题意: 产生从1-n的k个数的所有组合,按升序排列并返回. 思路: DFS一遍即可解决.注意升序. 1 class Solution { 2 public: 3 vector<vector<int>> ans; 4 int nn, kk; 5 void DFS(vector<int>& seq,int n,int cnt) 6 { 7 if(cnt==kk) ans.push_back(seq); 8 else if(n>nn) return ; 9 el

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

[LeetCode] Subsets (bfs的vector实现)

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [

Clone Graph leetcode java(DFS and BFS 基础)

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

Leetcode之深度优先搜索(DFS)专题-DFS+记忆化 329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].

Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)

Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合. 给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系.如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道.你必须输出所有学生

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string