首页 > 解决方案 > 递归方法的理解(排列)

问题描述

正如标题所述,我正在寻找一些帮助来理解附加代码中发生的事情。具体来说,为什么'(“”,s)'中有引号是怎么回事(String s1,String s2),这是字母被实例化的地方吗?

最后,底部的 displayPermutaion 方程到底发生了什么?

感谢您花时间提供帮助,我真的很感激!

public static void main(String[] args)
        {

        System.out.print("Please enter your phrase here: ");
        String s = new Scanner(System.in).next();
        System.out.println("All the permutations of your phrase are listed below:\n" + s);

        displayPermutation(s);   
        }

    public static void displayPermutation(String s)
        {
        displayPermutation("", s);
        }

    public static void displayPermutation(String s1, String s2)
           {
        if (s2.length() == 0)
                   {
            System.out.println(s1);
                   }
            else
                   {
                for (int i = 0; i < s2.length(); i++)
                   {
                   displayPermutation(s1 + s2.charAt(i), s2.substring(0, i) + s2.substring(i+1));
                   }
            }
        }
    }
    

标签: javarecursionpermutation

解决方案


推荐阅读