Java——全排列(输出结果按照ASCII码值进行排序)

import java.util.Arrays;
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        char[] str = in.nextLine().toCharArray();
        Arrays.sort(str);
        printStr(str, 1, "");
    }
    private static void printStr(char[] str, int deepth, String printStr) {
        if (deepth == str.length) 
        {
            for (int i = 0; i < str.length; i++)
                if (str[i] != 'n')
                    System.out.println(printStr + str[i]);
            return;
        }
        for (int i = 0; i < str.length; i++) 
        {
            boolean noRep = true;
            for (int j = 0; j < i; j++)
                if (str[i] == str[j])
                    noRep = false;
            if (str[i] != 'n' && noRep)
            {
                char[] strC = new char[str.length];
                for (int j = 0; j < str.length; j++)
                    strC[j] = str[j];
                strC[i] = 'n';
                printStr(strC, deepth + 1, printStr + str[i]);
            }
        }
    }
}

 

你可能感兴趣的:(我的编程笔记,Java)