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:

  1. [
  2. [3],
  3. [1],
  4. [2],
  5. [1,2,3],
  6. [1,3],
  7. [2,3],
  8. [1,2],
  9. []
  10. ]

问题: 给定一个集合,求集合元素的所有组合的情况。

实际上就是一题求全组合的题目。

nums[i...n) 的所有组合情况可以分为两种:包含nums[i] 的 和 不包含 nums[i] 的。

  • 包含 nums[i] 的:nums[i] 依次加到 nums[i+1...n) 的全部情况即可。
  • 不包含 nums[i] 的 :就是 nums[i+1...n) 的全部情况。

上面的递推关系,实际上就是 DP 思路。

  1. vector<vector<int>> theset;
  2.  
  3. void regardValue(int value){
  4.  
  5. if (theset.size() == ) {
  6. vector<int> tmp0;
  7. vector<int> tmp1 = {value};
  8. theset.push_back(tmp0);
  9. theset.push_back(tmp1);
  10. return;
  11. }
  12.  
  13. int LofPre = (int)theset.size();
  14.  
  15. for (int i = ; i < LofPre; i++) {
  16. vector<int> tmp = theset[i];
  17. tmp.push_back(value);
  18. theset.push_back(tmp);
  19. }
  20. }
  21.  
  22. vector<vector<int>> subsets(vector<int>& nums) {
  23.  
  24. std::sort(nums.begin(), nums.end());
  25.  
  26. for (int i = ; i < nums.size(); i++) {
  27. regardValue(nums[i]);
  28. }
  29.  
  30. return theset;
  31. }

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:

  1. [
  2. [2],
  3. [1],
  4. [1,2,2],
  5. [2,2],
  6. [1,2],
  7. []
  8. ]

问题:若集合中包含重复值,求所有的可能组合,即子集合。

要求:子集合内可以有重复值,但是子集合之间不可以有重复的子集合。

同样采用原来的思路,用 unordered_set<vector<int>> 代替 vector<vector<int>> ,就得最后结果后,再转为 vector<vector<int>> 即可。

在实现过程中发现 unordered_set<vector<int>> 不能直接使用。在 stackoverflow 看到解法方法,增加对 vector<int> 结构进行 hash 即可使用。修改后,算法实现并通过。

  1. struct VectorHash {
  2. size_t operator()(const vector<int>& v) const {
  3. hash<int> hasher;
  4. size_t seed = ;
  5. for (int i : v) {
  6. seed ^= hasher(i) + 0x9e3779b9 + (seed<<) + (seed>>);
  7. }
  8. return seed;
  9. }
  10. };
  11.  
  12. vector<vector<int>> theset;
  13.  
  14. unordered_set<vector<int>, VectorHash> uniSet;
  15.  
  16. void regardValue(int value){
  17.  
  18. if (uniSet.size() == ) {
  19. vector<int> tmp0;
  20. vector<int> tmp1 = {value};
  21. uniSet.insert(tmp0);
  22. uniSet.insert(tmp1);
  23. return;
  24. }
  25.  
  26. unordered_set<vector<int>, VectorHash> cpSet = uniSet;
  27.  
  28. unordered_set<vector<int>, VectorHash>::iterator t_iter;
  29.  
  30. for (t_iter = cpSet.begin(); t_iter != cpSet.end(); t_iter++) {
  31. vector<int> tmp = *t_iter;
  32.  
  33. tmp.push_back(value);
  34. uniSet.insert(tmp);
  35. }
  36. }
  37.  
  38. vector<vector<int>> subsetsWithDup(vector<int>& nums) {
  39.  
  40. sort(nums.begin(), nums.end());
  41.  
  42. for (int i = ; i < nums.size(); i++) {
  43. regardValue(nums[i]);
  44. }
  45.  
  46. unordered_set<vector<int>, VectorHash>::iterator t_iter;
  47.  
  48. for (t_iter = uniSet.begin(); t_iter != uniSet.end(); t_iter++) {
  49. vector<int> tmp = *t_iter;
  50. theset.push_back(tmp);
  51. }
  52.  
  53. return theset;
  54.  
  55. }

[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. sublime2 Ctags 快捷键

    Commands Listing Command Key Binding Alt Binding Mouse Binding rebuild_ctags ctrl+t, ctrl+r     navi ...

  2. char *s = getpass()屏幕不回显示 ,返回输入的字符

    char *s = getpass(“please input you name:”)屏幕不回显示 ,返回输入的字符

  3. C#XML创建与节点对象引用

    我们在创建xml过程中会遇到不同的级别有相同节点的情况.如下面的xml: <?xml version="1.0" encoding="GBK"> & ...

  4. 转载的在DOS下操作mysql

    转载原文地址:http://www.server110.com/mysql/201309/1070.html 一.连接MYSQL. 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1 ...

  5. Python中的redis学习笔记

    redis是一个key-value结构的数据库,value的格式可以使string,set,list,map(即python里面的dict),sorted set(有序集合) 1.初始化 1)直接连接 ...

  6. POJ 3371 Flesch Reading Ease 无聊恶心模拟题

    题目:http://poj.org/problem?id=3371 无聊恶心题,还是不做的好,不但浪费时间而且学习英语. 不过为了做出点技术含量,写了个递归函数... 还有最后判断es,ed,le时只 ...

  7. POJ 1035 Spell checker 简单字符串匹配

    在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...

  8. 加密算法 - RSA算法二

    RSA算法原理(二)  声明: 本文转自阮一峰 (http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html) 有了这些知识, ...

  9. bzoj 3043: IncDec Sequence 模拟

    3043: IncDec Sequence Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 248  Solved: 139[Submit][Statu ...

  10. apache asp.net

    http://www.apache.org/dist/httpd/ http://server.it168.com/server/2005-10-11/20051011027201.shtml htt ...