题目:

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. CF113D 高斯消元、dp

    题目链接 https://codeforces.com/contest/113/problem/D 思路 \(k[i]=\frac{1-p[i]}{ru[i]}\) f[i][j]表示经过i和j的次数 ...

  2. Newcoder Metropolis(多源最短路 + Dijkstra堆优化)题解

    题目链接:https://www.nowcoder.com/acm/contest/203/I?tdsourcetag=s_pcqq_aiomsg来源:牛客网 思路:我们用用fa[i]表示距离i最近的 ...

  3. C语言 字符串大小写转换 自定义函数

    #include <stdio.h>#include <stdlib.h>#include <string.h> char * strtolower(char * ...

  4. 论文笔记:Parallel Tracking and Verifying: A Framework for Real-Time and High Accuracy Visual Tracking

    Parallel Tracking and Verifying: A Framework for Real-Time and High Accuracy Visual Tracking  本文目标在于 ...

  5. 对象扩展运算符(…)与rest运算符

    对象扩展运算符(…) 当编写一个方法时,我们允许它传入的参数是不确定的.这时候可以使用对象扩展运算符来作参数,看一个简单的列子: function xzdemo(...arg){ console.lo ...

  6. [nginx] - 使用nginx实现反向代理,动静分离,负载均衡,session共享

    反向代理概念 先说正向代理,比如要访问youtube,但是不能直接访问,只能先找个FQ软件,通过FQ软件才能访问youtube. FQ软件就叫做正向代理.所谓的反向代理,指的是用户要访问youtube ...

  7. maven下载jar包下载不下来的解决方法

    转载请注明出处: 在eclipse中安装了maven插件,项目在运行的时候,一直通过pom.xml文件下载jar包,一直下载不下来, 在更新maven库时,如果网络问不定或者是一些自己手动安装到本地m ...

  8. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  9. C语言 深入学习

    浮点数: x = Mx*2^Ex为一个规格化浮点数,Mx为x的尾数,Ex为x的阶码. 1e-6:表示1 * 10 ^ (-6). 编译时执行: sizeof是运算符(而非函数),在编译时执行,不会导致 ...

  10. github删除某个库repository

    1.登陆gihub网站,在该选中需要删除的repository,点击进去 2.删除repository 点击进去以后进入新的页面,拉到页面尾部,如图 然后弹出确认框,再输入需要删除的repositor ...