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. Sql server 浅谈用户定义表类型

    1.1 简介 SQL Server 中,用户定义表类型是指用户所定义的表示表结构定义的类型.您可以使用用户定义表类型为存储过程或函数声明表值参数,或者声明您要在批处理中或在存储过程或函数的主体中使用的 ...

  2. javascript face ++

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. BFC与hasLayout之间的故事

    刚拒绝了一个很有诱惑的公司,不是不想去,而是对现在的能力还不确定,希望能够进一步提高自己的技能,所有想写博客了,监督自己的学习进度·········现在还没有开放博客,希望成熟一些后再开放吧! 进入正 ...

  4. H5小内容(四)

    SVG   基本内容     SVG并不属于HTML5专有内容       HTML5提供有关SVG原生的内容     在HTML5出现之前,就有SVG内容     SVG,简单来说就是矢量图     ...

  5. JS分段上传文件(File)并使用MD5.js加密文件段用来后台校验

    HTML <form method="POST" name="form1" action="/mupload/upload/" enc ...

  6. PHP分页初探 一个最简单的PHP分页代码实现

    PHP分页代码在各种程序开发中都是必须要用到的,在网站开发中更是必选的一项. 要想写出分页代码,首先你要理解SQL查询语句:select * from goods limit 2,7.PHP分页代码核 ...

  7. thinkphp+mysql+bootstrap

    #thinkphp+mysql+bootstrapthinkphp3.2.3,bootstrap V3一个简易的企业cms网站系统,只要将clients的host改为对应的域名即可.thinkphp. ...

  8. 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误

    这是因为EF6.0重构了一些命名空间后,和VS原有的实体数据模型模板不一致了(ObjectContext context = ((IObjectContextAdapter)dataContext). ...

  9. CodeFirst数据库迁移小记

    打开“程序包管理器控制台”菜单项一.Enable-Migrations -ContextTypeName Code_First_数据迁移.Models.T_DbContext成功后提示:已在项目“Co ...

  10. __attribute__ 详解

    GNU C的一大特色(却不被初学者所知)就是__attribute__机制.__attribute__可以设置函数属性(Function    Attribute).变量属性(Variable Att ...