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) 解题思路,即全组合算法的更多相关文章

  1. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  2. [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 ...

  3. [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 ...

  4. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. ubuntu下virtualenv的复制

    将一个用户下的virtualenv复制到另一个用户下.直接复制无法使用,source后的python依旧是系统自带的python. 原始env名:/home/llx/work/nn/nnenv.bak ...

  2. 如何实现.so共享库文件

    .so共享库相当于window中的.DLL文件 两个进程同时调用了.so文件,进程就会加载的.so文件到各自的内存空间,而不能实现进程间通讯. .so文件编译的方法: -so文件不需要main文件,即 ...

  3. jQuery DataTables 插件使用笔记

    初始化 在页面中 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type=&q ...

  4. REST接口规范

    参考文章 这篇文章使用不同的method代表不同操作 http://www.cnblogs.com/tommyli/p/3913018.html 实际应用中(我们过去的应用) 则是直接使用url来代表 ...

  5. PHP设计模式之适配器模式

    将一个类的接口转换成客户希望的另一个接口,适配器模式使得原本的由于接口不兼容而不能一起工作的那些类可以一起工作.应用场景:老代码接口不适应新的接口需求,或者代码很多很乱不便于继续修改,或者使用第三方类 ...

  6. Avoiding “will create implicit index” NOTICE

    执行PgSql避免 notice 信息,执行之前加入以下语句调整报错级别即可: SET CLIENT_MIN_MESSAGES = ‘WARNING’;

  7. 大量字段表单在PHP便捷处理分享

    关于程序开发中的表单批量提交策略很多时候一个表单太多的字段,如何能够高效获取表单字段,也为如何提神开发的效率和统一性? 比如一个系统的某个有26个字段,那么我用表单的名称用26个a到z的字母, 你是选 ...

  8. iOS开发之自定义画板

    今天整好有时间, 写了一个自定义的画板!  [我的github] GLPaint主要采用QuartzCore框架, 对画布上的元素进行渲染, 然后通过UIImageWriteToSavedPhotos ...

  9. adb logcat 查看日志

    使用 logcat 命令 查看和跟踪系统日志缓冲区的命令logcat的一般用法是: [adb] logcat [<option>] ... [<filter-spec>] .. ...

  10. 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...