简体   繁体   English

谁能告诉我这段代码是如何工作的?

[英]Can anyone tell me how is this code working?

I can't figure out how this recursion problem works.我无法弄清楚这个递归问题是如何工作的。

I can only get to the point where it prints "teal" after that I get lost.在我迷路之后,我只能到达它打印“蓝绿色”的地步。

class Anagrams {

    private static void printAnagramsHelper(String prefix, String word) {

        if (word.length() <= 1) {
            System.out.println(prefix + word);
        } else {

            printAnagramsHelper(
                                prefix + word.charAt(0),
                                word.substring(1)
                                );

            for (int i = 1; i <= word.length() - 2; i++) {
                printAnagramsHelper(
                                    prefix + word.charAt(i),
                                    word.substring(0, i) + word.substring(i + 1)
                                   );
            }

            printAnagramsHelper(
                                prefix + word.charAt(word.length() - 1),
                                word.substring(0, word.length() - 1)
                                );
        }
    }

    public static void printAnagrams(String word) {
        printAnagramsHelper("", word);
    }

    public static void main(String[] args) {
        printAnagrams("teal");
    }
}

Output is correct but cant figure out how did it come. Output 是正确的,但无法弄清楚它是怎么来的。

The “prefix” is the result built so far, and “word” is the pool of letters to add the prefix. “prefix”是到目前为止构建的结果,“word”是添加前缀的字母池。

The terminating case is when there are no more letters in “word”, so the result (“prefix”) is printed and recursion stops.终止情况是“word”中没有更多字母,因此打印结果(“prefix”)并且递归停止。

There are three ways of recursing:递归的三种方式:

  1. Add the first letter of “word” to “prefix” and recurse with the rest of “word”将“word”的第一个字母添加到“prefix”并用“word”的rest递归
  2. Iterate from the 2nd letter of “word” to the 2nd last letter, recurse with that letter “removed” (by adding substrings either side of the removed letter together)从“word”的第二个字母到倒数第二个字母,用“removed”的那个字母递归(通过在被删除的字母的两边添加子串)
  3. Add the last letter of “word” to “prefix” and recurse with the preceding letters of “word”将“word”的最后一个字母添加到“prefix”并与“word”的前面字母进行递归

The code is the java implementation of the above description.代码就是上面描述的java实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能告诉我如何改善这段代码的运行时间? - Can anyone tell me how to improve the run time of this code? 谁能告诉我这段代码出了什么问题? - Can anyone tell me what's going wrong with this code? 谁能告诉我为什么在我的 evaulateExpression 中使用 pop() 不起作用? - Can anyone tell me why the use of pop() in my evaulateExpression is not working? 任何人都可以告诉我这个伪代码怎么了? - Anyone can tell me what's wrong with this pseudo code? 有谁知道告诉我这段代码是否可重构? 如果是这样怎么办? - does anyone know tell me if this code is refactorable? And if so how? 谁能告诉我如何在此下拉菜单上训练Selenium WebDriver? - Can anyone tell me how to train Selenium WebDriver on this dropdown menu? 谁能告诉我如何在javacode中运行此命令? - Can anyone tell me how to run this command in javacode? 谁能告诉我,如何使用 Java 脚本代码将下面的 html 下拉值保存到表格中 - Can anyone please tell me, how to save below html drop down values into table by using Java script Code 你能告诉我这个程序如何运作吗? - Can you tell me how this program is working? 谁能告诉我为什么这个计算为0? - Can anyone tell me why this is evaluating to 0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM