你这个程序太精炼了,不太看得懂。。
用你的例子改了一下,结果不对,哪里错了
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MySolution6 {
private static List<List<Integer[]>> helper(List<Integer[]> bookSet,int curIndex,int level, int num) {
List<List<Integer[]>> ret = new ArrayList<>();
List<Integer[]> bookSetCombination=new ArrayList<>();
if(num>bookSet.size()) return ret ;
if(num==1) {
for (Integer[] book : bookSet)
{
bookSetCombination.add(book.clone());
ret.add(bookSetCombination);
}
return ret;
}
if (num == (level+1)) {
for (Integer[] book : bookSet)
{
bookSetCombination.add(book.clone());
}
ret.add(bookSetCombination);
for (List<Integer[]> combination: ret) {
for (Integer[] book : combination)System.out.printf(" %s", Arrays.deepToString(book));
System.out.println();
};
return ret;
}
for (int i = curIndex; i < bookSet.size(); i++) {
bookSetCombination.add(bookSet.get(i));
ret=helper(bookSet, i + 1, level + 1, num);
}
return ret;
}
public static void main(String[] args) {
Integer[][] books1 = new Integer[][] { { 3, 10 }, { 4, 20 }, { 1,5 }, { 2, 40 },{3,40} };
List<Integer[]> bookSet = new ArrayList<Integer[]>(Arrays.asList(books1));
int num=bookSet.size();
while(num-->=0) {
List<List<Integer[]>> ret = helper(bookSet,0,0,num);
for (List<Integer[]> combination: ret) {
for (Integer[] book : combination)System.out.printf(" %s", Arrays.deepToString(book));
System.out.println();
};
}
}
}
【 在 callmebbser 的大作中提到: 】
: import java.util.Arrays;
: import java.util.TreeSet;
: public class CombinationTest {
: ...................
--
修改:iStudy FROM 58.37.127.*
FROM 58.37.127.*