[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 ...
随机推荐
- 常用命令su ls cp cd mv cat touch mkdir rm head less more pwd tac 等
1.用户切换 su:switch user su kevin //半切换,切换到kevin用户,但是不读取kevin用户的配置文件 su - kevin //完全切换,执行这个命令的时候表示切 ...
- 用C#实现MD5算法
/// <summary> /// 一个实现MD5散列字符串的类 /// </summary> public sealed class MD5Hash ...
- javascript获取url参数的方法
发布:thatboy 来源:Net [大 中 小] 本文介绍下,在javascript中取得url中某一个参数的方法,这里分享一个小例子,供大家学习参考下.本文转自:http://www. ...
- eval 如何定义函数
eval(compile('''def fun(): print 'bbb' ''', '<string>', 'exec')) fun()
- STM32之外部中断控制
一.STM32外部中断 1.STM32外部中断结构图 如上图所示:主要包括四个环节,GPIO.AFIO.EXTI.NVIC.以STM32F103VE(100脚)为例说明硬件模块的数量: GPIO: ...
- 图片Base64编码 简单使用
图片在线转换Base64,图片编码base64 http://tool.css-js.com/base64.html HTML5 + js <input type="file" ...
- Error Code: 1064 – You have an error in your SQL syntax解决几个方法
本文转自 http://www.anyiwa.com/?p=1066 Error Code: 1064 – You have an error in your SQL syntax解决几个方法 十一月 ...
- win7 任务计划 任务映像已损坏或篡改(异常来自HRESULT:0x80041321)
转自win7 任务计划 任务映像已损坏或篡改(异常来自HRESULT:0x80041321) 请这样操作: 1. 以管理员身份运行命令提示符并执行命令 chcp 437 schtasks /query ...
- JAVA与编译语言及解释语言的关系
转自JAVA结合了编译和解释执行的优点 编译型语言是一次性编译成机器码,脱离开发环境独立运行,所以运行效率较高,但是由于编译成的是特定平台上机器码,所以可移植性差. 编译型语言的典型代表有C.C++. ...
- POJ 1778 All Discs Considered(拓扑排序)
点我看题目 题意 :其实题意我也说不清楚,因为比赛的时候我盯着看了1个小时也没看懂....就是两个磁盘,第一个有n1的安装包,编号为1~n1,第二个有n2个安装包,编号为n1~n2.给你d对关系,(x ...