Recursion 这个递归函数是如何返回的;ArrayofStrings";在主体中,尤其是在循环中,只有索引更新,我可以';我看不到任何字符串
关于这个问题的一些细节:在下面的方法中,我无法弄清楚递归方法如何返回字符串数组,但它只更新内部for循环中的索引,并且我看不到任何文本内容Recursion 这个递归函数是如何返回的;ArrayofStrings";在主体中,尤其是在循环中,只有索引更新,我可以';我看不到任何字符串,recursion,Recursion,关于这个问题的一些细节:在下面的方法中,我无法弄清楚递归方法如何返回字符串数组,但它只更新内部for循环中的索引,并且我看不到任何文本内容 public static ArrayList<String> getPermutations (String word,ArrayList<Integer> indices) { int length = word.length(); // Create the empty ArrayList to return
public static ArrayList<String> getPermutations (String word,ArrayList<Integer> indices) {
int length = word.length();
// Create the empty ArrayList to return
ArrayList<String> toReturn = new ArrayList<String>();
// If all characters are considered, we are done. Return empty ArrayList
if (indices.size() == length) {
toReturn.add("");
return toReturn;
}
// Iterate over each character to find the permutation
for (int i = 0; i < length; i++) {
// Skip if index is already considered
if (indices.contains(i)) continue;
// Create and populate a duplicate ArrayList of indices that have already been considered
ArrayList<Integer> newIndices = new ArrayList<Integer>();
for (int index : indices) {
newIndices.add(index);
}
// Add current index
newIndices.add(i);
// Calculate permutations
ArrayList<String> permutations = Permutations.getPermutations(word, newIndices);
// Add the current character before the permutations of the rest of the word
for (String str : permutations) {
toReturn.add(word.charAt(i) + str);
}
}
// Return
return toReturn;
}
公共静态ArrayList getPermutations(字符串字、ArrayList索引){
int length=word.length();
//创建要返回的空ArrayList
ArrayList toReturn=新的ArrayList();
//如果考虑了所有字符,我们就完成了。返回空的ArrayList
if(index.size()=长度){
t返回。加上(“”);
回归回归;
}
//迭代每个字符以查找排列
for(int i=0;i