gpt4 book ai didi

Java 字谜递归 List> 仅存储空列表

转载 作者:行者123 更新时间:2023-12-02 07:28:42 25 4
gpt4 key购买 nike

嗨,在这个递归方法中,我试图找到所有字谜并将其添加到列表中>,但是当我运行此代码时会发生什么,它只是返回很多空列表。

      private List<List<String>> findAnagrams(LetterInventory words,
ArrayList<String> anagram, int max,
Map<String, LetterInventory> smallDict, int level, List<List<String>> result) {

ArrayList<String> solvedWord = new ArrayList<String>();
LetterInventory shell;
LetterInventory shell2;
if (level < max || max == 0) {
Iterator<String> it = smallDict.keySet().iterator();
while (it.hasNext()) {
String k = it.next();
shell = new LetterInventory(k);
shell2 = words;
if (shell2.subtract(shell) != null) {
anagram.add(k);
shell2 = words.subtract(shell);
if (shell2.isEmpty()) {
//System.out.println(anagram.toString()); it prints off fine here
result.add(anagram); // but doesnt add here

}
else

findAnagrams(shell2, anagram, max, smallDict, level + 1, result);
anagram.remove(anagram.size()-1);
}
}
}
return results;
}

最佳答案

我猜它就在这里。

else 
findAnagrams(shell2, anagram, max, smallDict, level + 1, result);
anagram.remove(anagram.size()-1); // this is outside of the else case

每次添加元素时,都会从anagram 中删除该元素。您是否打算将其放在 else 中?

else {
findAnagrams(shell2, anagram, max, smallDict, level + 1, result);
anagram.remove(anagram.size()-1);
}

关于Java 字谜递归 List<List<String>> 仅存储空列表 <Strings>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13204110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com