77. Combinations (java 求C(n,k)的组合,排除重复元素)
题目:
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)的组合,排除重复元素)的更多相关文章
- java集合 collection-list-ArrayList 去除ArrayList集合中的重复元素。
import java.util.*; /* 去除ArrayList集合中的重复元素. */ class ArrayListTest { public static void sop(Object o ...
- 77. Combinations (JAVA)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Java 去除 ArrayList 集合中的重复元素
// One practice package Collection; import java.util.ArrayList; import java.util.Iterator; // 去除 Arr ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Java求字符串中出现次数最多的字符
Java求字符串中出现次数最多的字符 [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/51933611 Java ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- 给定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) 输出: 一个整数. ...
- UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]
题意: 求矩形覆盖K次以上的面积 分析: k很小,可以开K颗线段树,用sum[rt][i]来保存覆盖i次的区间和,K次以上全算K次 // File Name: 11983.cpp // Author: ...
- POJ2761---Feed the dogs (Treap求区间第k大)
题意 就是求区间第k大,区间 不互相包含. 尝试用treap解决一下 第k大的问题. #include <set> #include <map> #include <cm ...
随机推荐
- P1552 [APIO2012]派遣
链接 https://www.luogu.org/problemnew/show/P1552 思路 忍者数量肯定越多越好 那就从下到上的合并它的孩子 左偏树的话 顺便维护一个tot,大头堆,如果tot ...
- Jenkins serving Cake: our recipe for Windows
https://novemberfive.co/blog/windows-jenkins-cake-tutorial/ Where we started, or: why Cake took the ...
- 未能加载文件或程序集“System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKe
https://bbs.csdn.net/topics/392046946 电脑没安装mvc4,应该是,解决这个问题一上午了今天,然后装完了后就好了! https://www.microsoft.co ...
- asp.net mvc 加三层架构 完美搭配
http://www.hysql.org/aspnet/20180630/5712.html 先来一张项目的层级结构图: Model:模型层,主要是各种类型.枚举以及ORM框架,框架完成数据库和实体类 ...
- p1654 OSU!
期望DP 设\(g[i]\)表示前i个的连续1的期望长度,\(h[i]\)表示前i个连续1的长度的平方的期望,\(f[i]\)表示前i个的期望得分 由期望的线性性质,我们可以考虑统计新增一个对答案的贡 ...
- (转)How Hash Algorithms Work
本文转自:http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software Home Posted: Novembe ...
- BZOJ 4399 魔法少女LJJ(线段树合并)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=4399 思路 码农题,需要一定代码功底.方法很暴力,先将权值离散,表示在线段树里储存的位置,每 ...
- 【Net Core】DNX概述
1. 什么是.NET执行环境 ? .NET Execution Environment(DNX) 是一个SDK 和运行时环境,它包含所有的你需要创建和运行.net应用程序的组件.它提供一个主机进程,C ...
- 利用C#实现AOP常见的几种方法详解
利用C#实现AOP常见的几种方法详解 AOP面向切面编程(Aspect Oriented Programming) 是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 下面这篇文章主要 ...
- 【转载】SeleniumIDE入门
http://www.open-open.com/lib/view/open1452488109558.html