任意字符序列,输出它们所有的排列组合
思路:利用递归进行解决,这里没有考虑字符串中含有重复字符的情况,当字符串长度为1的时候,输出字符,否则递归调用函数
1 public class Testa { 2 public static void main(String[] args) { 3 String str = "xafdvs"; 4 getString("", str); 5 } 6 7 private static void getString(String former, String latter) { 8 if (latter.length() == 1) { 9 System.out.println(former + latter); 10 } else { 11 int len = latter.length(); 12 for (int i = 0; i < len; i++) { 13 String temp = latter.substring(i, i + 1); 14 String tf = former + temp; 15 String tl = latter.replace(temp, ""); 16 getString(tf, tl); 17 } 18 } 19 } 20 }
来源:http://blog.163.com/liangming_08/blog/static/11365676320128211235927/