LeetCode:Combinations这篇博客中给出了不包含重复元素求组合的5种解法。我们在这些解法的基础上修改以支持包含重复元素的情况。对于这种情况,首先肯定要对数组排序,以下不再强调

修改算法1:按照求包含重复元素集合子集的方法LeetCode:Subsets II算法1的解释,我们知道:若当前处理的元素如果在前面出现过m次,那么只有当前组合中包含m个该元素时,才把当前元素加入组合

class Solution {
public:
void combine(vector<int> &vec, int k) {
if(k > vec.size())return;
sort(vec.begin(), vec.end()); vector<int>tmpres;
helper(vec, 0, k, 0, tmpres);
} //从vec的[start,vec.size()-1]范围内选取k个数,tmpres是当前组合
//times是上一个元素出现的次数
void helper(vector<int> &vec, int start, int k, int times, vector<int> &tmpres)
{
if(vec.size()-start < k)return;
if(k == 0)
{
for(int i = 0; i < tmpres.size(); i++)
cout<<tmpres[i]<<" ";
cout<<endl;
return;
}
if(start == 0 || vec[start] != vec[start-1])//当前元素前面没有出现过
{
//选择vec[start]
tmpres.push_back(vec[start]);
helper(vec, start+1, k-1, 1, tmpres);
tmpres.pop_back();
//不选择vec[start]
helper(vec, start+1, k, 1, tmpres);
}
else//当前元素前面出现过
{
if(tmpres.size() >= times && tmpres[tmpres.size()-times] == vec[start])
{
//只有当tmpres中包含times个vec[start]时,才选择vec[start]
tmpres.push_back(vec[start]);
helper(vec, start+1, k-1, times+1, tmpres);
tmpres.pop_back();
}
helper(vec, start+1, k, times+1, tmpres);
}
}
};

从[1,2,2,3,3,4,5]中选3个的结果如下:


修改算法2:同理,可以得到代码如下                    本文地址

class Solution {
public:
void combine(vector<int> &vec, int k) {
if(k > vec.size())return;
sort(vec.begin(), vec.end()); vector<int>tmpres;
helper(vec, 0, k, 0, tmpres);
} //从vec的[start,vec.size()-1]范围内选取k个数,tmpres是当前组合
//times是上一个元素出现的次数
void helper(vector<int> &vec, int start, int k, int times, vector<int> &tmpres)
{
if(vec.size()-start < k)return;
if(k == 0)
{
for(int i = 0; i < tmpres.size(); i++)
cout<<tmpres[i]<<" ";
cout<<endl;
return;
}
for(int i = start; i <= vec.size()-k; i++)
{
if(i == 0 || vec[i] != vec[i-1])//当前元素前面没有出现过
{
times = 1;
//选择vec[i]
tmpres.push_back(vec[i]);
helper(vec, i+1, k-1, 1, tmpres);
tmpres.pop_back();
}
else//当前元素前面出现过
{
times++;
//vec[i]前面已经出现过times-1次
if(tmpres.size() >= times-1 && tmpres[tmpres.size()-times+1] == vec[i])
{
//只有当tmpres中包含times-1个vec[i]时,才选择vec[i]
tmpres.push_back(vec[i]);
helper(vec, i+1, k-1, times, tmpres);
tmpres.pop_back();
}
}
}
}
};

修改算法3:算法3是根据LeetCode:Subsets 算法2修改未来,同理我们也修改LeetCode:SubsetsII 算法2

class Solution {
public:
void combine(vector<int> &vec, int k) {
if(k > vec.size())return;
sort(vec.begin(), vec.end()); vector<vector<int> > res(1);//开始加入一个空集
int last = vec[0], opResNum = 1;//上一个数字、即将要进行操作的子集数量
for(int i = 0; i < vec.size(); ++i)
{
if(vec[i] != last)
{
last = vec[i];
opResNum = res.size();
}
//如果有重复数字,即将操作的子集的数目和上次相同
int resSize = res.size();
for(int j = resSize-1; j >= resSize - opResNum; j--)
{
res.push_back(res[j]);
res.back().push_back(vec[i]);
if(res.back().size() == k)//找到一个大小为k的组合
{
for(int i = 0; i < res.back().size(); i++)
cout<<res.back()[i]<<" ";
cout<<endl;
}
}
}
}
};

对于算法4和算法5,都是基于二进制思想,这种解法不适用与包含重复元素的情况

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3695463.html

从n个元素中选择k个的所有组合(包含重复元素)的更多相关文章

  1. 【算法30】从数组中选择k组长度为m的子数组,要求其和最小

    原题链接:codeforce 267 Div2 C 问题描述: 给定长度为n的数组a[],从中选择k个长度为m的子数组,要求和最大. 形式描述为:选择$k$个子数组[$l_1$, $r_1$], [$ ...

  2. JS 验证数组中是否包含重复元素

    验证JS中是否包含重复元素,有重复返回true:否则返回false 方案一. function isRepeat(data) { var hash = {}; for (var i in data) ...

  3. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  4. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  5. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  6. leetcode-219-Contains Duplicate II(使用set来判断长度为k+1的闭区间中有没有重复元素)

    题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...

  7. Java思考——HashSet集合如何保证元素的唯一性也就是不包含重复元素?

    首先将源码逐级找出来1.HashSet<String> hs=new HashSet<String>();         hs.add("hello"); ...

  8. 从N个元素中抽取K个不重复元素(抽奖问题)

    核心就是 把N数组抽中的元素给K数组 把N数组最后一位给N数组被抽走的那一位(这时候N数组最后一位元素和被抽走的那位元素值相等) 把N数组长度减一,去除最后一位

  9. 在程序中对ArrayList进行排序,并剔除重复元素

    import java.util.*; class sortDemo { public static void main(String[] args) { ArrayList<Object> ...

随机推荐

  1. hdu1561 树形dp,依赖背包

    多重背包是某个物品可以选择多次,要把对物品数的枚举放在对w枚举外面 分组背包是某组的物品只能选一个,要把对每组物品的枚举放在对w枚举内侧 依赖背包是多层的分组背包,利用树形结构建立依赖关系,每个结点都 ...

  2. [JSOI2009]等差数列

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1558 题解: 考虑这么用线段树进行维护,由于他有区间修改等差数列 很容易想到可以用差分数组来维 ...

  3. BZOJ1209 [HNOI2004]最佳包裹 三维凸包 计算几何

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1209 题目概括 给出立体的n个点.求三维凸包面积. 题解 增量法,看了一天,还是没有完全懂. 上板 ...

  4. Ubuntu (虚拟机同样) 更换内核?

    1:查看当前安装的内核dpkg -l|grep linux-image 2:查看可以更新的内核版本:sudo apt-cache search linux-image 3:安装新内核sudo apt- ...

  5. Ubuntu18.04更换国内源

    Ubuntu18.04更换国内源 Ubuntu本身的源使用的是国内的源,下载速度比较慢,不像CentOS一样yum安装的时候对镜像站点进项选择, 所以选择了更换成国内的源. 以下内容整合自网络 备份/ ...

  6. 洛谷 [P1024]一元三次方程求解【二分答案】

    题目链接:https://www.luogu.org/problemnew/show/P1024 题目描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b ...

  7. POJ-1456 Supermarket 销售商品【贪心】+【并查集】

    题目链接:http://poj.org/problem?id=1456 题目大意: 有N件商品,分别给出商品的价值和销售的最后期限,只要在最后日期之前销售处,就能得到相应的利润,并且销售该商品需要1天 ...

  8. Linux学习之挂载光盘和U盘(六)

    Linux下挂载光盘和U盘 挂载 linux下硬盘.U盘.软盘.移动硬盘都必须挂载后才能使用,不过硬盘的挂载是系统自动进行的. linux中每一个硬件都有一个设备文件名,就是将U盘什么的设备文件名与挂 ...

  9. Reinforcement Learning 的那点事——强化学习(一)

    引言 最近实验室的项目需要用到强化学习的有关内容,就开始学习起强化学习了,这里准备将学习的一些内容记录下来,作为笔记,方便日后忘记了好再方便熟悉,也可供大家参考.该篇为强化学习开篇文章,主要概括一些有 ...

  10. 获取img的高

    我们可以通过css设置图片的width,然后通过 clientWidth获取图片的宽,但是这个宽似乎是css里面定义的width值,但是对于图片的高,使用 clientHeight 来获取似乎是有些问 ...