[LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
78. Subsets
Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
问题: 给定一个集合,求集合元素的所有组合的情况。
实际上就是一题求全组合的题目。
nums[i...n) 的所有组合情况可以分为两种:包含nums[i] 的 和 不包含 nums[i] 的。
- 包含 nums[i] 的:nums[i] 依次加到 nums[i+1...n) 的全部情况即可。
- 不包含 nums[i] 的 :就是 nums[i+1...n) 的全部情况。
上面的递推关系,实际上就是 DP 思路。
vector<vector<int>> theset;
void regardValue(int value){
if (theset.size() == ) {
vector<int> tmp0;
vector<int> tmp1 = {value};
theset.push_back(tmp0);
theset.push_back(tmp1);
return;
}
int LofPre = (int)theset.size();
for (int i = ; i < LofPre; i++) {
vector<int> tmp = theset[i];
tmp.push_back(value);
theset.push_back(tmp);
}
}
vector<vector<int>> subsets(vector<int>& nums) {
std::sort(nums.begin(), nums.end());
for (int i = ; i < nums.size(); i++) {
regardValue(nums[i]);
}
return theset;
}
90. Subsets II
Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
问题:若集合中包含重复值,求所有的可能组合,即子集合。
要求:子集合内可以有重复值,但是子集合之间不可以有重复的子集合。
同样采用原来的思路,用 unordered_set<vector<int>> 代替 vector<vector<int>> ,就得最后结果后,再转为 vector<vector<int>> 即可。
在实现过程中发现 unordered_set<vector<int>> 不能直接使用。在 stackoverflow 看到解法方法,增加对 vector<int> 结构进行 hash 即可使用。修改后,算法实现并通过。
struct VectorHash {
size_t operator()(const vector<int>& v) const {
hash<int> hasher;
size_t seed = ;
for (int i : v) {
seed ^= hasher(i) + 0x9e3779b9 + (seed<<) + (seed>>);
}
return seed;
}
};
vector<vector<int>> theset;
unordered_set<vector<int>, VectorHash> uniSet;
void regardValue(int value){
if (uniSet.size() == ) {
vector<int> tmp0;
vector<int> tmp1 = {value};
uniSet.insert(tmp0);
uniSet.insert(tmp1);
return;
}
unordered_set<vector<int>, VectorHash> cpSet = uniSet;
unordered_set<vector<int>, VectorHash>::iterator t_iter;
for (t_iter = cpSet.begin(); t_iter != cpSet.end(); t_iter++) {
vector<int> tmp = *t_iter;
tmp.push_back(value);
uniSet.insert(tmp);
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
for (int i = ; i < nums.size(); i++) {
regardValue(nums[i]);
}
unordered_set<vector<int>, VectorHash>::iterator t_iter;
for (t_iter = uniSet.begin(); t_iter != uniSet.end(); t_iter++) {
vector<int> tmp = *t_iter;
theset.push_back(tmp);
}
return theset;
}
[LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法的更多相关文章
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] Course Schedule I (207) & II (210) 解题思路
207. Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
- leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的反复II) 解题思路和方法
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 74. Search a 2D Matrix 解题思路
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 347. Top K Frequent Elements 解题思路 - Java
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [LeetCode] 307. Range Sum Query - Mutable 解题思路
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- 嵌入Web资源的方法
可以将js .图片.css等嵌入Assembly中,这样就不用将文件在aspx中写了,特别适合做自定义控件的时候将控件用到的资源打包. 将文件放到项目的合适路径,比如jpg文件所在路径的namespa ...
- JavaScript—W3school
一.JavaScript基础 1.写入HTML输出 2.对事件作出反应 3.改变HTML内容 4.改变HTML图像 5.改变HTML样式 6.验证输入 <script> Function ...
- JavaScript语言用10张图
JavaScript 语言基础知识点总结,用图片树形结构说明.包括Windows对象.JavaScriptDOM基本操作.JavaScript变量.JavaScript数据类型.JavaScript运 ...
- less学习-浏览器端编译(一)
demo地址 http://www.qq210.com/shoutu/android 1.下载less包,官网 2.引入less文件 <link rel="stylesheet/les ...
- 检查ftp备份数据完整性及短信告警的shell脚本
发布:thebaby 来源:net [大 中 小] 检查ftp备份数据完整性及短信告警的shell,有需要的朋友可以参考下. 该脚本实现如下的功能: 对远程备份到ftp服务器的数据完整性及 ...
- CentOS 6.4 使用第三方源
1.EPEL源 CentOS 6.x 32-bit (x86/i386):rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/ep ...
- FusionCharts xml入门教程
由于项目需求需要做一个报表,选择FusionCharts作为工具使用.由于以 前没有接触过报表,网上也没有比较详细的fusionCharts教程,所以决定好好研究FusionCharts,同时做一个比 ...
- [BUGFIX]__import_pywin32_system_module__
import_pywin32_system_module 修复方法: 编辑 X:/Python27/Lib/site-packages/win32/lib/pywintypes.py 第114行 if ...
- IBM总架构师寇文东谈程序员的职业规划
有些年轻的程序员向我咨询,将来的路该怎么走?俗话说,条条大路通罗马.不同的路都能走向成功,到底选择哪条路,取决于自己的兴趣.可能有程序员会问:如果还没有找到自己的兴趣怎么办?我的建议是多尝试,努力做, ...
- 集成Facebook和Twitter的Model动画-b
这个动画. 感谢大神分享 JPPopPresentAnimation.gif 要实现这个功能分两步走:第一步,先实现这个动画.第二步,0行代码集成到项目.也就是,你不用改原有项目的任何代码,只要把写 ...