理解递归时遇到的问题 - Java

发布于 2024-10-31 14:29:08 字数 1152 浏览 1 评论 0原文

我从 this 问题中获取了代码,我在 Eclipse 中运行了它,代码很好,但是我对递归顺序内部如何进行感到困惑。

public class Permute {
    public static void main(String[] args) throws IOException {
        System.out.println("Enter a string");
        BufferedReader bufReader = new BufferedReader(new InputStreamReader(
                System.in));
        String text = bufReader.readLine();
        shuffle("", text);
    }

    public static void shuffle(String dummy, String input) {
        if (input.length() <= 1)
            System.out.println(dummy + input);
        else {
            for (int i = 0; i < input.length(); i++) {
                input = input.substring(i, i + 1) + input.substring(0, i)
                        + input.substring(i + 1);
                shuffle(dummy + input.substring(0, 1), input.substring(1));

            }
        }
    }
}

我发现很难理解 Shufflefor 循环中的递归。有解码递归步骤的指针吗?

编辑:好的,这是我的理解,假设我的输入是 ABC ,当我在第一个循环中运行时,我得到虚拟 = A 和输入 = BC ,所以立即采取的步骤是沿着输入 = BC 和虚拟 = A 的递归,然后返回迭代 i 以获取初始输入?

I got the code from this question, I ran it in Eclipse and the code was fine, but I confused myself in how the recursion order goes internally.

public class Permute {
    public static void main(String[] args) throws IOException {
        System.out.println("Enter a string");
        BufferedReader bufReader = new BufferedReader(new InputStreamReader(
                System.in));
        String text = bufReader.readLine();
        shuffle("", text);
    }

    public static void shuffle(String dummy, String input) {
        if (input.length() <= 1)
            System.out.println(dummy + input);
        else {
            for (int i = 0; i < input.length(); i++) {
                input = input.substring(i, i + 1) + input.substring(0, i)
                        + input.substring(i + 1);
                shuffle(dummy + input.substring(0, 1), input.substring(1));

            }
        }
    }
}

I found difficulty in understanding recursion in the for loop of Shuffle. Any pointers in decoding the recursion steps?

EDIT : Okay this is my understanding, say suppose my input is ABC and when i run in the first loop , i get dummy = A and input = BC, so the immediate step would be to go down the recursion for input = BC and dummy = A and then come back to iterate i for the initial input ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哥,最终变帅啦 2024-11-07 14:29:08

添加全局计数器:

static int depth = 0; /* calling depth of the recursive method */

添加为 shuffle 的第一行:

System.out.printf("%" + depth++ + "s call dummy='%s' input='%s'\n", "", dummy, input);

添加为 shuffle 的最后一行:

System.out.printf("%" + --depth + "s return\n", "");

运行程序。现在您可以看到会发生什么。

Add a global counter:

static int depth = 0; /* calling depth of the recursive method */

Add as the first line of shuffle:

System.out.printf("%" + depth++ + "s call dummy='%s' input='%s'\n", "", dummy, input);

Add as the last line of shuffle:

System.out.printf("%" + --depth + "s return\n", "");

Run the program. Now you can see what happens.

捶死心动 2024-11-07 14:29:08

将其视为将工作分成步骤。您的 shuffle 函数采用两个参数,dummy 表示已打乱的字符串部分,input 表示仍打乱的字符串部分。必须重新洗牌。

在每一步中,您都会对 input 的第一个字符进行洗牌:

        for (int i = 0; i < input.length(); i++) {
            input = input.substring(i, i + 1) + input.substring(0, i)
                    + input.substring(i + 1);

然后,递归地应用算法,已经洗牌的部分会更长:

            shuffle(dummy + input.substring(0, 1), input.substring(1));

直到不再需要洗牌:

    if (input.length() <= 1)
        System.out.println(dummy + input);

Think of it as dividing the job in steps. Your shuffle function takes two arguments, dummy for the part of the string that is already shuffled, and input for the part of the string that still has to be shuffled.

At every step, you shuffle the first character of input:

        for (int i = 0; i < input.length(); i++) {
            input = input.substring(i, i + 1) + input.substring(0, i)
                    + input.substring(i + 1);

and then, recursively apply the algorhythm, with the part already shuffled being a character longer:

            shuffle(dummy + input.substring(0, 1), input.substring(1));

Until there is nothing more to shuffle:

    if (input.length() <= 1)
        System.out.println(dummy + input);
情场扛把子 2024-11-07 14:29:08

它递归地按输入长度彻底混洗输入。因此,一旦每次递归将字符串打乱第 i 个项后,它就会返回。

这将是一个采用大 O 表示法的 n 平方复杂度算法。

如果没有调试器,改组是很棘手的;)

It exhaustively shuffles the input by the inputs length, recursively. So once each recursion has shuffled the string by the i'th term, it returns.

This would be an n-squared complexity algorithm in big-O notation.

The shuffling is tricky to work out without a debugger ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文