简体   繁体   English

这个递归排列代码是如何工作的?

[英]How does this recursive permutation code work?

public class Permutation 
{ 
    public static void main(String[] args) 
    { 
        String str = "ABC"; 
        int n = str.length(); 
        Permutation permutation = new Permutation(); 
        permutation.permute(str, 0, n-1); 
    } 

    private void permute(String str, int l, int r) 
    { 
        if (l == r) 
            System.out.println(str); 
        else
        { 
            for (int i = l; i <= r; i++) 
            { 
                str = swap(str,l,i);     
                permute(str, l+1, r); ``
                str = swap(str,l,i); 
            } 
        } 
    } 
public String swap(String a, int i, int j) 
    { 
        char temp; 
        char[] charArray = a.toCharArray(); 
        temp = charArray[i] ; 
        charArray[i] = charArray[j]; 
        charArray[j] = temp; 
        return String.valueOf(charArray); 
    } 
} 

I don't understand this code how this is going to work generating the permutations of a string我不明白这段代码如何生成字符串的排列

 str = swap(str,l,i);     
 permute(str, l+1, r);
 str = swap(str,l,i); 

The permute method prints to standard output all strings that can be obtained from str by permuting the characters in the indices from l to r inclusive. permute方法打印到标准 output 通过将索引中的字符从l置换到r可以从str获得的所有字符串。 It is assumed that l <= r .假设l <= r

Does this answer your question?这回答了你的问题了吗? Well, at least it's a very central part of the answer.好吧,至少它是答案的核心部分。

I believe that markspace has a good point: study your recursive method running in your debugger.我相信标记空间有一个好处:研究在调试器中运行的递归方法。 Watch the call stack grow and shrink and inspect the stack frames and the variables in them.观察调用堆栈的增长和收缩,并检查堆栈帧和其中的变量。

In the meantime my contribution is the way I think about recursive methods.与此同时,我的贡献是我思考递归方法的方式。 To me writing a recursive method is a special case of writing code that calls a method that has not yet been implemented.对我来说,编写递归方法是编写调用尚未实现的方法的代码的一种特殊情况。 Trusting that it can and will be implemented later.相信它可以并且将在以后实施。 I hope you're familiar with this way of writing code.我希望您熟悉这种编写代码的方式。

My first paragraph above specifies the behaviour of the permute method.我上面的第一段指定了permute方法的行为。 Let's study how the implementation realizes this behaviour.让我们研究一下实现是如何实现这种行为的。

First, if l and r are equal, that is, they point to the same character, there is no swapping needed since there is only one permutation, the one where that character is already in its right place.首先,如果lr相等,即它们指向同一个字符,则不需要交换,因为只有一个排列,即该字符已经在正确位置的排列。 So print str and exit.所以打印str并退出。

If l is strictly less than r , the method in turn puts every character in the range into position l .如果l严格小于r ,则该方法依次将范围内的每个字符放入 position l中。 This is necessary to obtain all permutations.这是获得所有排列所必需的。 For each character put at l it permutes all the remaining characters in all possible ways in the remaining positions, that is l + 1 through r .对于放在l的每个字符,它以所有可能的方式在剩余位置置换所有剩余字符,即l + 1r How does it do this?它是如何做到的? Well, we have specified a method that can do it for us, so just call that method.好吧,我们已经指定了一个可以为我们做这件事的方法,所以只需调用那个方法。 If it confuses you that the method we call is the method we're already in, try not to think of that fact for now.如果它让您感到困惑,我们调用的方法是我们已经使用的方法,请暂时不要考虑这个事实。 Pretend we're calling a different method that we trust to do the job.假设我们正在调用我们信任的不同方法来完成这项工作。 Finally, after the call to that method we swap the characters back into their original posistions so we're ready for the next time through the loop.最后,在调用该方法之后,我们将字符交换回原来的位置,以便为下一次循环做好准备。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM