[Back] arrangement of classic string

 
 
Links: HTTPS: // www.nowcoder.com/questionTerminal/fe6b651b66ae47d7acce78ffdd9a96c7 
Source: cattle off network 

Import java.util.ArrayList;
 Import java.util.List;
 Import java.util.Collections;
 public  class Solution {
     public ArrayList <String> Permutation (String STR) { 
        List <String> = resultList new new the ArrayList <> ();
         IF (str.length () == 0 )
             return (the ArrayList) resultList;
         // recursive initial value (str array, empty list initial subscript 0) 
        Fun (str.toCharArray (), resultList, 0 );
        The Collections.sort (resultList); 
        return (the ArrayList) resultList; 
    } 
     
    Private  void Fun ( char [] CH, List <String> List, int i) {
         // This is the recursive termination condition, the subscript i has been moved is char when the end of the array, consider adding the result set of strings into the 
        iF (I-== ch.length. 1 ) {
             // determine what repeat 
            iF (! list.contains ( new new string (CH))) { 
                List .add ( new new String (CH));
                 return ; 
            } 
        } the else {
             // this section is backtracking herein to "abc" Example 
             
            //Thought and recursive stack pushing and popping are the same, one state after the end of the return encounter will be called back to the place of execution to continue 
             
            // 1. This is the first time into the ch = [ 'a ',' b ',' c '], list = [], i = 0, I referred state A, i.e., the initial state
             // then j = 0, swap (ch, 0,0), that is, [' a ' , 'b', 'c' ], into the recursion, tune their own, but i is 1, the state after the exchange (0,0) is called a state where I B 
             // i is not equal to 2, here, j = 1, implementation of a swap (ch, 1,1), this state is called state I C1, and then enter the function fun, this time labeled T1, i is 2, then the time to enter one if, the "abc "put in list 
            //////////// / -------" At this time, the result set to [ "ABC"] 
             
            // 2. after completion of execution list.add, return is encountered , fall back to T1, the next execution of the second swap (ch, 1,1), and restore the state C1 to state B
             // after completion of recovery, the for loop continues, then j = 2, then the swap (ch , 1,2), to give "acb", I call this state C2, then perform fun, this time labeled T2, found i + 1 = 2, it is also added to the result set, then fall back to return T2 down at the execution 
            //////// //// / ------- "At this time, the result set to [" abc "," acb "
            
             
            //              A | B | C (state A)
             //                |
             //                | the swap (0,0)
             //                |
             //              A | B | C (state B)
             //              / \
             //    the swap (1,1) / \ swap (1,2) (state C1 and state C2)
             //            / \
             //          A | B | CA | C | B 
             
            // 3. after return to state A, the for loop continues, j = 1, i.e., swap ( ch, 0,1), i.e., "BAC", this state may be called again to state a, following the steps above 
            //////////// / ------- "At this time, the result set is [ "abc", "acb" , "bac", "bca"] 
             
            //              A | B | C (state A)
             //                |
            //                | the swap (0,1)
             //                |
             //              B | A | C (state B)
             //              / \
             //    the swap (1,1) / \ the swap (1,2) (state C1 and state C2)
             //            / \
             //          B | A | CB | C | A 
             
            // 4. continue for loop, j = 2, i.e. swap (ch, 0,2), i.e., "cab", this state may be called again to state A , the following steps above 
            //////////// / ------- "At this time, the result set to [" abc "," acb " ," bac "," bca "," cab "," CBA "] 
             
            //              A | B | C (state A)
             //                |
             //                | the swap (0,
            2)
            //               |
            //              C | B | A (state B)
             //              / \
             //    the swap (1,1) / \ the swap (1,2) (state C1 and state C2)
             //            / \
             //          C | B | AC | A | b 
             
            // 5. Finally, exit the for loop end. 
             
            for ( int J = I; J <ch.length; J ++ ) { 
                the swap (CH, I, J); 
                Fun (CH, List, I + 1'd ); 
                the swap (CH, I, J); 
            } 
        } 
    } 
     
    // two switching elements of the array subscripts 
    Private  void the swap ( char [] STR, int I,int j) {
            if (i != j) {
                char t = str[i];
                str[i] = str[j];
                str[j] = t;
            }
        }
    }

 

Guess you like

Origin www.cnblogs.com/twoheads/p/11261463.html