[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 ...
随机推荐
- sublime2 Ctags 快捷键
Commands Listing Command Key Binding Alt Binding Mouse Binding rebuild_ctags ctrl+t, ctrl+r navi ...
- char *s = getpass()屏幕不回显示 ,返回输入的字符
char *s = getpass(“please input you name:”)屏幕不回显示 ,返回输入的字符
- C#XML创建与节点对象引用
我们在创建xml过程中会遇到不同的级别有相同节点的情况.如下面的xml: <?xml version="1.0" encoding="GBK"> & ...
- 转载的在DOS下操作mysql
转载原文地址:http://www.server110.com/mysql/201309/1070.html 一.连接MYSQL. 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1 ...
- Python中的redis学习笔记
redis是一个key-value结构的数据库,value的格式可以使string,set,list,map(即python里面的dict),sorted set(有序集合) 1.初始化 1)直接连接 ...
- POJ 3371 Flesch Reading Ease 无聊恶心模拟题
题目:http://poj.org/problem?id=3371 无聊恶心题,还是不做的好,不但浪费时间而且学习英语. 不过为了做出点技术含量,写了个递归函数... 还有最后判断es,ed,le时只 ...
- POJ 1035 Spell checker 简单字符串匹配
在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...
- 加密算法 - RSA算法二
RSA算法原理(二) 声明: 本文转自阮一峰 (http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html) 有了这些知识, ...
- bzoj 3043: IncDec Sequence 模拟
3043: IncDec Sequence Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 248 Solved: 139[Submit][Statu ...
- apache asp.net
http://www.apache.org/dist/httpd/ http://server.it168.com/server/2005-10-11/20051011027201.shtml htt ...