简体   繁体   中英

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. It is assumed that 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. 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. So print str and exit.

If l is strictly less than r , the method in turn puts every character in the range into 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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