字符串的排列

题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Solution {
    
    private Set set = new HashSet();
    private ArrayList array = new ArrayList();
    public ArrayList Permutation(String str) {
        
        if(str == null || str.length() ==0) {
            
            return array;
        }
        int length = str.length();
        char[] c = new char[length];
        for(int i = 0; i < length; i++) {
            
            c[i] = str.charAt(i);
        }
        char[] temp = new char[length];
        int[] a = new int[length];
        recursion(c,temp,a,length,0);
        return array;
    }
    private void recursion(char[] c, char[] temp, int[] a, int length, int k) {
        
        if(k == length) {
            
            String str = new String(temp);
            if(!set.contains(str)){
                
                array.add(str);
                set.add(str);
            }
        }else {
            
            for(int i = 0; i < length; i++) {
                
                if(a[i] == 1) {
                    
                    continue;
                }else {
                    
                    a[i] = 1;
                    temp[k] = c[i];
                    recursion(c,temp,a,length, k + 1);
                    a[i] = 0;
                }
            }
        }
        
    }
    
}

你可能感兴趣的:(字符串的排列)