public class AllOrderWithinNumbers {
public static void main(String...args){
int[] i = new int[]{1,2,4,5};
AllOrderWithinNumbers t = new AllOrderWithinNumbers();
ArrayList al = t.getOrders(i);
System.out.println(al.size());
System.out.println(al);
}
private ArrayList getOrders(int[] others){
ArrayList al = new ArrayList();
//如果只有一个数,则返回自身
if(others.length==1){
al.add(others[0]);
}else{
for(int i=0;i<others.length;i++){
int[] t = this.getLeftNumber(others,i);
ArrayList tmp = this.getOrders(t);
//tmp.add(0,others[i]); //uncomment this line and comment the blow line, to see what the result is
this.linkSubOrders(others[i],tmp);
//这里要进行判断,如果tmp中包含ArrayList,那么直接addAll,把每一个ArrayList作为子元件插入到al中,如果tmp不包含ArrayList,那么要把tmp作为一个整体插入
// al.add(tmp);// this line and the next 4 lines, you can compare the differences
if(tmp.get(0) instanceof ArrayList){
al.addAll(tmp);
}else{
al.add(tmp);
}
}
}
return al;
}
/**
*
* @param i 开头数
* @param tmp 除了开头数之外其它数字组成的排序,由于在多个ArrayList中,因此要把开头数插入到每个ArrayList开头
* 注意:由于tmp中会存在嵌套的ArrayList,因此这里要进行嵌套ArrayList递归,如[1,[2,[3,4][4,3]],[3,[2,4][4,2]]...]要转换成[1,2,3,4],[1,2,4,3],[1,3,2,4]...这样的形式
*/
private void linkSubOrders(int i, ArrayList tmp) {
if(tmp.get(0) instanceof ArrayList){
for(int j=0;j<tmp.size();j++){
ArrayList al = (ArrayList) tmp.get(j);
this.linkSubOrders(i, al);
}
}else{
tmp.add(0,i);
}
}
/**
* copy others to a new array, not including the i-th number
* @param others
* @param i
* @return
*/
private int[] getLeftNumber(int[] others, int i) {
int[] t = new int[others.length-1];
for(int j=0;j<others.length;j++){
if(j<i){
t[j] = others[j];
}
if(j==i){
}
if(j>i){
t[j-1] = others[j];
}
}
return t;
}
}
补:群里坦克大牛的一种写法,非常简洁:
private void test(){
char[] dictionaries = "ABC".toCharArray();
int limit = 1 << dictionaries.length;
for (int mask = 1; mask < limit; mask++) {
for (int submask = 1, i = 0; i < dictionaries.length; submask <<= 1, i++) {
if ((mask & submask) != 0) {
System.out.print(dictionaries[i]);
}
}
System.out.println();
}
}
思路:首先知道所有组合数是2^n-1,也就是limit的大小, 对limit进行循环,这样就可以取出所有的组合了,注意submask,它是每循环一次左移一位,然后和mask进行&
分享到:
相关推荐
从一组数据中 返回组合的和等于某个值 的所有组合
@Weekday 算出一周中的某一天,返回一个表示这一天的数字 @Word 从一个文本字符串里返回指定的单词 @Year 从指定的时间-日期值中提取年份 @Yes 返回值 1 @Yesterday 返回与昨天日期相对应的时间-日期值 @Zone 返回...
如何提取一串数字中的几位数字(字符) 如何把一个单元格中的数字挑出来 分割文本 按照给定的位数,截断小数点后的数字 单元格数字提取问题 以关键字提取名称 如何把文本中的前几个字符去除 对一列中的文字统一去掉...
如何提取一串数字中的几位数字(字符) 如何把一个单元格中的数字挑出来 分割文本 按照给定的位数,截断小数点后的数字 单元格数字提取问题 以关键字提取名称 如何把文本中的前几个字符去除 对一列中的文字统一去掉...
也许你能用数学办法推出鱼的条数,但我们的要求你编出一个程序,让计算机帮你算出鱼的总数。 16. 试编程找出能被各位数字之和整除的一切两位数。 17. 一个正整数的个位数字是6,如果把个位数字移到首位,所得到的...
这个串由三个字符以及跟随在字母之后的一位数字构成.这些复杂的模式使用的正则表达式语法指定了该表达式中每个元素要重复出现的次数. 指定复制的字符总是出现在它们所作用的模式后面.由于某种复制类型相当常用....
6� 每一个基本操作称为一条指令�而解算某一问题的一串指令序列�称为程序。 7� 取指周期中从内存读出的信息流是指令流�而在执行器周期中从内存读出的信息流是指 令流。 8� 半导体存储器称为内存�存储容量更大...
当采用方式①时,必须显式罗列出所有要包含在派生集中的成员,并且罗列的每个成员必须属于稠密集。使用前面的例子,显式罗列派生集的成员: allowed(product,machine,week)/A M 1,A N 2,B N 1/; 如果需要生成一个大...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 八. ...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 八. ...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 19、Jdo是什么? 20、什么是spring的IOC AOP 21、STRUTS的工作流程! 22、spring 与EJB的区别!! 八. 软件工程与设计...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 八. ...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 八. ...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 八. ...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 八. ...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 ...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 137 19、Jdo是什么? 137 20、什么是spring的IOC AOP 137 21、STRUTS的工作流程! 137 22、spring 与EJB的区别!! 137 八. ...