博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Subsets
阅读量:5041 次
发布时间:2019-06-12

本文共 3337 字,大约阅读时间需要 11 分钟。


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
> subsets(vector
& nums) { 4 sort(nums.begin(), nums.end()); 5 vector
> subs; 6 vector
sub; 7 genSubsets(nums, 0, sub, subs); 8 return subs; 9 }10 void genSubsets(vector
& nums, int start, vector
& sub, vector
>& 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
> subsets(vector
& nums) { 4 sort(nums.begin(), nums.end()); 5 vector
> subs(1, vector
()); 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
> subsets(vector
& nums) { 4 sort(nums.begin(), nums.end()); 5 int num_subset = pow(2, nums.size()); 6 vector
> res(num_subset, vector
()); 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 :-) 

转载于:https://www.cnblogs.com/jcliBlogger/p/4548112.html

你可能感兴趣的文章
C#线程入门
查看>>
CSS清除浮动方法
查看>>
JVM内存回收机制简述
查看>>
洛咕 P2480 [SDOI2010]古代猪文
查看>>
js-创建对象的几种方式
查看>>
JDK JRE Java虚拟机的关系
查看>>
2018.11.20
查看>>
word20161215
查看>>
12th week blog
查看>>
dijkstra (模板)
查看>>
python小记(3)
查看>>
编译Linux驱动程序 遇到的问题
查看>>
大型分布式网站架构技术总结
查看>>
HDU 1017[A Mathematical Curiosity]暴力,格式
查看>>
[算法之美] KMP算法的直观理解
查看>>
EntityFramework 性能优化
查看>>
【ASP.NET开发】菜鸟时期的ADO.NET使用笔记
查看>>
android圆角View实现及不同版本号这间的兼容
查看>>
OA项目设计的能力③
查看>>
Cocos2d-x3.0 文件处理
查看>>