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 ...
随机推荐
- 差分数组|小a的轰炸游戏-牛客317E
小a的轰炸游戏 题目链接:https://ac.nowcoder.com/acm/contest/317/E 思路 这题考查的是对差分数组原理和前缀和的理解. 四个数组分别记录朝着四个方向下放的个数 ...
- Python hasattr() 函数 // python中hasattr()、getattr()、setattr()函数的使用
http://www.runoob.com/python/python-func-hasattr.html https://www.cnblogs.com/zanjiahaoge666/p/74752 ...
- nohup 让进程在后台可靠运行的几种方法
1. nohup nohup 无疑是我们首先想到的办法.顾名思义,nohup 的用途就是让提交的命令忽略 hangup 信号. nohup 的使用是十分方便的,只需在要处理的命令前加上 nohup 即 ...
- 案例:8,64,256都是2的阶次方数(8是2的3次方),用Java编写程序来判断一个整数是不是2的阶次方数。
如果一个数是2的阶次方数,则它的二进制数的首位一般是1,后面全为0.比如8:1000,64:1000000,如果将这个数减1后再作与&运算,则应该全为0,(x&(x-1)==0&am ...
- Latex 算法过长 分页显示方法
参考: Algorithm tag and page break Latex 算法过长 分页显示方法 1.引用algorithm包: 2.在\begin{document}前加上以下Latex代码: ...
- Python数据类型补充2
四.列表 常用操作+内置的方法: 1.按索引存取值(正向存取+反向存取):即可存也可以取 # li=['a','b','c','d'] # print(li[-1]) # li[-1]='D' # p ...
- HDU 4320 Arcane Numbers 1(质因子包含)
http://acm.hdu.edu.cn/showproblem.php?pid=4320 题意: 给出A,B,判断在A进制下的有限小数能否转换成B进制下的有限小数. 思路: 这位博主讲得挺不错的h ...
- sprinf sprintf_s 的用法
函数功能: 将数据格式化输出到字符串 函数原型: int sprintf( char *buffer, const char *format [,argument] ... ) 注意这里的buffer ...
- 进度条的制作-python
import time,sys def view_bar(num, total): rate = float(num) / float(total) rate_num = int(rate * 100 ...
- markdown一些网站
1.https://stackedit.io/editor 2.https://github.com/bioinformatist/LncPipeReporter 3.