题目:

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

解析:同求全部组合的过程一样,只是这里限制每个组合中元素的个数为k,求所有组合时并不限制元素个数。

若要排除重复的元素,则首先对所有元素进行排序。

代码:

   public static List<List<Integer>> getAllCombinations(int[] array,int k){
Arrays.sort(array); //排序,为了在接下来求组合时排除重复组合
List<List<Integer>> list = new ArrayList<List<Integer>>();
List<Integer> item = new ArrayList<Integer>();
dfs_repeated(list, item, 0, array, k);
return list;
}
    /**
* 存在重复元素,解决方法参考subsetsII
* 求子集的算法同求重复元素的算法原理是相同的!
* 首先对数组元素进行排序
* {1,2,2}的所有组合有 1,2,12,122
*/
public static void dfs_repeated(List<List<Integer>> list, List<Integer> item, int start, int[] array,int k){ if(item.size() == k)
list.add(new ArrayList<Integer>(item));
int i = start;
while(i < array.length){
item.add(array[i]);
dfs_repeated(list, item, i + 1, array, k);
item.remove(item.size() - 1);
i++;
//排除重复的元素,直到找到一个不重复的元素时再添加
while(i < array.length && array[i] == array[i - 1])
i++;
}
}

77. Combinations (java 求C(n,k)的组合,排除重复元素)的更多相关文章

  1. java集合 collection-list-ArrayList 去除ArrayList集合中的重复元素。

    import java.util.*; /* 去除ArrayList集合中的重复元素. */ class ArrayListTest { public static void sop(Object o ...

  2. 77. Combinations (JAVA)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. Java 去除 ArrayList 集合中的重复元素

    // One practice package Collection; import java.util.ArrayList; import java.util.Iterator; // 去除 Arr ...

  4. leetCode 77.Combinations (组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. Java求字符串中出现次数最多的字符

    Java求字符串中出现次数最多的字符  [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/51933611      Java ...

  6. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  7. 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。

    题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...

  8. UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]

    题意: 求矩形覆盖K次以上的面积 分析: k很小,可以开K颗线段树,用sum[rt][i]来保存覆盖i次的区间和,K次以上全算K次 // File Name: 11983.cpp // Author: ...

  9. POJ2761---Feed the dogs (Treap求区间第k大)

    题意 就是求区间第k大,区间 不互相包含. 尝试用treap解决一下 第k大的问题. #include <set> #include <map> #include <cm ...

随机推荐

  1. luoguP4072 [SDOI2016]征途

    [SDOI2016]征途 大体 大概就是推推公式,发现很傻逼的\(n^3\)DP get60 进一步我们发现状态不能入手,考虑优化转移 套个斜率优化板子 每一层转移来一次斜率优化 思路 先便便式子 \ ...

  2. 完全卸载oraclean安装

    完全卸载oracle11g步骤:1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务.2. 开始->程序->Oracle - OraHome ...

  3. (转)Awesome GAN for Medical Imaging

    Awesome GAN for Medical Imaging 2018-08-10 09:32:43 This blog is copied from: https://github.com/xin ...

  4. (转载)C# winform 在一个窗体中如何设置另一个窗体的TextBox的值

    方法1:修改控件的访问修饰符.(不建议使用此法) public System.Windows.Forms.TextBox textBox1; 在调用时就能直接访问 Form1 frm = new Fo ...

  5. 剥开比原看代码10:比原是如何通过/create-key接口创建密钥的

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  6. hadoop的Linux操作

    初学hadoop之linux系统操作的hdfs的常用命令 Hadoop之HDFS文件操作 Hadoop fs命令详解 官网doc sudo su - hdfs:免密,以hdfs账户登陆.可操作hdfs ...

  7. ISE14.7兼容性问题集锦https://www.cnblogs.com/ninghechuan/p/7241371.html

    ISE14.7兼容性问题集锦 对于电子工程师来说,很多电路设计仿真软件都是特别大的,安装下来一般都是上G,甚至几十G,而且win7的兼容性也是最好的,不愿意升级win10是因为麻烦,而且没有必要,对于 ...

  8. [UVA-11100] The Trip

    题目大意 大箱子能装小箱子,求在满足最少箱子的情况下,最小化每个箱子中最大的箱子个数. 解析 想到二分枚举箱子数,然后贪心的选择放进箱子的位置. 最优策略一定是将最大的 \(m\) 个先找出来,然后把 ...

  9. Java处理微信公众号文章图片不显示微信

    http://blog.csdn.net/just4you/article/details/52933620

  10. Linux CentOS 7 安装mongoDB(4.0.6)

    在官网下载安装包: https://www.mongodb.com/download-center/community 或者通过wget下载 wget https://fastdl.mongodb.o ...