字符串的排列

    xiaoxiao2021-12-14  23

    题目描述

    输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。  
    输入描述:
    输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。

    import java.util.ArrayList; import java.util.Collections; public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<String> result = new ArrayList<String>(); if (str.length() == 0 || str == null) { return result; } char[] in = str.toCharArray(); Permutation(result, in, 0, in.length); Collections.sort(result); return result; } public static void Permutation(ArrayList<String> list, char[] in, int start, int end) { if (start == end - 1) { String result = new String(in); if (!list.contains(result)) list.add(result); return; } for (int i = start; i < end; i++) { char temp1 = in[start]; char temp2 = in[i]; in[start] = in[i]; in[i] = temp1; Permutation(list, in, start + 1, end); in[start] = temp1; in[i] = temp2; } } }

    转载请注明原文地址: https://ju.6miu.com/read-971017.html

    最新回复(0)