好吧所以我们基本上有这个问题要回答,但我很困惑,不知道如何使用递归来获得所有可能的组合..请有人救我!
写一个public static method threadings
,其中int n
(代表每条项链上的珠子数量)和一套Strings
(代表可用珠子colours
;你的代码不得更改此Set),并返回ArrayLists
Strings
的集合,表示给定颜色的n个珠子可以穿线的所有顺序。如果n < 1
,则返回一个仅包含一个空集ArrayList
。
正确行为的例子:
•threadings(0, {red,green}) = {[]}
•threadings(1, {red,green}) = {[red],[green]}
•threadings(2, {red,green})
= {[red,red],[red,green],[green,red],[green,green]}
•threadings(3, {red}) = {[red,red,red]}
提示:尽管如此,您可能希望threadings
递归调用自身
任何正确的方法都可以获得满分。
这是我直到现在所写的:
public static HashSet<ArrayList<String>> threadings (int n, Set<String> colours){
HashSet<ArrayList<String>> result= new HashSet<ArrayList<String>>();
ArrayList<String> inresult= new ArrayList<String>();
String[] col= new String[colours.size()];
if (n==0){
result.add(inresult);
return result;
}else{
}
}
答案 0 :(得分:0)
试试这个:
java