[LeetCode] Subsets

简介: Recursive (Backtracking) This is a typical problem that can be tackled by backtracking. Since backtracking has a more-or-less similar template, so I do not give explanations for this method.

Recursive (Backtracking)

This is a typical problem that can be tackled by backtracking. Since backtracking has a more-or-less similar template, so I do not give explanations for this method.

 1 class Solution {
 2 public:
 3     vector<vector<int>> subsets(vector<int>& nums) {
 4         sort(nums.begin(), nums.end());
 5         vector<vector<int>> subs;
 6         vector<int> sub;
 7         genSubsets(nums, 0, sub, subs);
 8         return subs; 
 9     }
10     void genSubsets(vector<int>& nums, int start, vector<int>& sub, vector<vector<int>>& subs) {
11         subs.push_back(sub);
12         for (int i = start; i < nums.size(); i++) {
13             sub.push_back(nums[i]);
14             genSubsets(nums, i + 1, sub, subs);
15             sub.pop_back();
16         }
17     }
18 };

Iterative

This problem can also be solved iteratively. Take [1, 2, 3] in the problem statement as an example. The process of generating all the subsets is like:

  1. Initially: [[]]
  2. Adding the first number to all the existed subsets: [[], [1]];
  3. Adding the second number to all the existed subsets: [[], [1], [2], [1, 2]];
  4. Adding the third number to all the existed subsets: [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]].

Have you got the idea :-)

The code is as follows.

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

Bit Manipulation

This is the most clever solution that I have seen. The idea is that to give all the possible subsets, we just need to exhaust all the possible combinations of the numbers. And each number has only two possibilities: either in or not in a subset. And this can be represented using a bit.

There is also another a way to visualize this idea. That is, if we use the above example, 1appears once in every two consecutive subsets, 2 appears twice in every four consecutive subsets, and 3 appears four times in every eight subsets, shown in the following (initially the 8subsets are all empty):

[], [], [], [], [], [], [], []

[], [1], [], [1], [], [1], [], [1]

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

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

The code is as follows.

 1 class Solution {
 2 public:
 3     vector<vector<int>> subsets(vector<int>& nums) {
 4         sort(nums.begin(), nums.end());
 5         int num_subset = pow(2, nums.size()); 
 6         vector<vector<int> > res(num_subset, vector<int>());
 7         for (int i = 0; i < nums.size(); i++)
 8             for (int j = 0; j < num_subset; j++)
 9                 if ((j >> i) & 1)
10                     res[j].push_back(nums[i]);
11         return res; 
12     }
13 };

Well, just a final remark. For Python programmers, this may be an easy task in practice since the itertools package has a function combinations for it :-) 

目录
相关文章
LeetCode 90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
121 0
LeetCode 90. Subsets II
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
259 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
169 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
370 2
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
276 3
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口

热门文章

最新文章