大神,能帮我看这修改一下吗?
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MySolution5 {
private static List<List<Integer[]>> helper(List<Integer[]> bookSet, 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 == bookSet.size()) {
for (Integer[] book : bookSet) {
bookSetCombination.add(book.clone());
}
ret.add(bookSetCombination);
return ret;
}
if (num < bookSet.size()) {
List<Integer[]> tmpBookSet = new ArrayList<>(bookSet);
for (int i = 0; i < bookSet.size(); i++) {
bookSetCombination.add(bookSet.get(i).clone());
//注释掉这句,是可以运行的
ret = helper(tmpBookSet, num - 1);
for (List<Integer[]> combination : ret) {
bookSetCombination.addAll(combination);
ret.add(bookSetCombination);
}
}
}
return ret;
}
public static void main(String[] args) {
// {3,10} 是一个book元素,看成一个元素就行了。
// 希望输出结果如下:
// {[3,10]} {[4,20]} {[1,5]}
// {[ 3, 10 ], [ 4, 20 ]} {[ 4, 20 ], [ 1, 5 ]} {[ 3, 10 ], [ 1, 5 ]}
// {[ 3, 10 ], [ 4, 20 ], [ 1, 5 ]}
Integer[][] books1 = new Integer[][] { { 3, 10 }, { 4, 20 }, { 1, 5 } };
List<Integer[]> bookCombination = new ArrayList<Integer[]>(Arrays.asList(books1));
int num = bookCombination.size();
while (num-- >= 0) {
List<List<Integer[]>> ret = helper(bookCombination, 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.*