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 ...
随机推荐
- bzoj4709 柠檬 单调栈,DP,斜率优化
目录 前言吐槽 思路 错误 代码 /* 前言吐槽 我真的不知道是咋做的 不过大约就是栈的斜率优化 哪位大佬见识广,给看看吧(乞讨) 思路 s是值等于a[i]的前缀和 转移方程$f[i]=max(f[i ...
- SpringBoot 整合使用dubbo
这里主要是按照teaey作者的spring-boot-starter-dubbo框架进行一些变化的使用 依赖包: <dependency> <groupId>com.aliba ...
- First Steps: Command-line
This brief tutorial will teach how to get up and running with the Flyway Command-line tool. It will ...
- (转载)MySQl数据库-批量添加数据的两种方法
方法一:使用excel表格 方法二:使用insert语句(FileWriter批量写入) 使用excel表格 1.打开数据表,按照表的字段在excel中添加数据.注意:表中字段名必须和excel中的名 ...
- pgAdmin的数据恢复
DOC 本地添加server 1.设置备份.恢复的exe路径.一般在pgAdmin的安装路径下可以找到 2.恢复restore,备份backup
- 51nod 1437 迈克步(单调栈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437 题意: 思路: 单调栈题.求出以每个数为区间最大值的区间范围即可. ...
- 查看iis对应w3wp.exe显示的进程ID号
1.任务管理器,查看,选择列,选择PID(进程标识符) 2.通过cmd查询: 管理员身份运行cmd,跳转到C:\Windows\System32\inetsrv目录,然后运行appcmd list w ...
- 用python读写excel的强大工具:openpyxl
最近看到好几次群里有人问xlwt.wlrd的问题,怎么说呢,如果是office2007刚出来,大家用xlsx文件用不习惯,还可以理解,这都10年过去了喂,就算没有进化到office2016,还在用of ...
- hdu 3861 The King’s Problem trajan缩点+二分图匹配
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- JavaSE习题 第七章 常用实用类
问答题 1.怎样实例化一个Calendar对象? Calendar ca=Calendar.getInstance(); 2.Calendar对象调用set(1949,9,1)设置的年月日分别是多少? ...