java 字符串的全排列输出

本文介绍了一种使用递归实现字符串全排列的算法,并提供了一种利用Set去重的方法。通过插入字符并移动数组元素的方式,该算法能生成所有可能的字符串组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.sunjsp.com

  1. public class MyTestGameCore {    
  2. public static void listAll(char[] arr_Str) {    
  3. if (arr_Str.length <= 1) {    
  4. System.out.println(arr_Str[0]);    
  5. return;    
  6. }    
  7. doPerm(0 + 1, arr_Str, new char[] { arr_Str[0] });    
  8. }    
  9.   
  10. /*   
  11. * index:当前要进行挨个插位的字符下标 arr_All:给定的字符数组 arr_Already:当前索引前已排好的字符数组   
  12. */    
  13. private static void doPerm(int index, char[] arr_All, char[] arr_Already) {    
  14. if (index == arr_All.length - 1) {    
  15. for (int i = index; i >= 0; i--) {    
  16. System.out.println(new String(insertAt(arr_Already, i,    
  17. arr_All[index])));    
  18. }    
  19.   
  20. else {    
  21. for (int i = index; i >= 0; i--) {    
  22. doPerm(index + 1, arr_All, insertAt(arr_Already, i,    
  23. arr_All[index]));    
  24. }    
  25. }    
  26. }    
  27.   
  28. // 指定位置插入新的字符,并将原数组中的元素往后移动    
  29. private static char[] insertAt(char[] char_Arr, int index, char c) {    
  30. char[] tmp = new char[char_Arr.length + 1];    
  31. for (int i = 0, j = 0; i < tmp.length; i++, j++) {    
  32. if (i == index) {    
  33. tmp[index] = c;    
  34. j--;    
  35. else {    
  36. tmp[i] = char_Arr[j];    
  37. }    
  38. }    
  39. return tmp;    
  40. }    
  41.   
  42. public static void main(String[] args) throws IOException {    
  43. System.out.println("Please input a String!");    
  44. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    
  45. String str = br.readLine();    
  46.   
  47. char[] arr_Str = str.toCharArray();    
  48. listAll(arr_Str);    
  49.   
  50. }    
  51. }   

public class MyTestGameCore { public static void listAll(char[] arr_Str) { if (arr_Str.length <= 1) { System.out.println(arr_Str[0]); return; } doPerm(0 + 1, arr_Str, new char[] { arr_Str[0] }); } /* * index:当前要进行挨个插位的字符下标 arr_All:给定的字符数组 arr_Already:当前索引前已排好的字符数组 */ private static void doPerm(int index, char[] arr_All, char[] arr_Already) { if (index == arr_All.length - 1) { for (int i = index; i >= 0; i--) { System.out.println(new String(insertAt(arr_Already, i, arr_All[index]))); } } else { for (int i = index; i >= 0; i--) { doPerm(index + 1, arr_All, insertAt(arr_Already, i, arr_All[index])); } } } // 指定位置插入新的字符,并将原数组中的元素往后移动 private static char[] insertAt(char[] char_Arr, int index, char c) { char[] tmp = new char[char_Arr.length + 1]; for (int i = 0, j = 0; i < tmp.length; i++, j++) { if (i == index) { tmp[index] = c; j--; } else { tmp[i] = char_Arr[j]; } } return tmp; } public static void main(String[] args) throws IOException { System.out.println("Please input a String!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); char[] arr_Str = str.toCharArray(); listAll(arr_Str); } }

 

通过改变字符的插入位置,使用递归来实现全排列.

下面的代码是采用Set实现的全排列去重,原理与上面一样:

Java代码 复制代码
  1. public static Set<String> list(String str) {   
  2.     char[] array_all = str.toCharArray();   
  3.     if (array_all.length == 1) {   
  4.         System.out.println(array_all[0]);   
  5.         return null;   
  6.     }   
  7.     Set<String> set = new HashSet<String>();   
  8.     set = go(array_all, new char[] { array_all[0] }, 1);   
  9.     return set;   
  10. }   
  11.   
  12. public static Set<String> go(char[] all, char[] already, int index) {   
  13.     Set<String> set = new HashSet<String>();   
  14.     if (index == all.length - 1) {   
  15.         for (int i = index; i >= 0; i--) {   
  16.             set.add(new String(insertINDEX(already,all[index],i)));   
  17.         }   
  18.     } else {   
  19.         for(int i = index; i >= 0; i--){   
  20.             set.addAll(go(all,insertINDEX(already,all[index],i),index+1));   
  21.         }   
  22.     }   
  23.     return set;   
  24. }   
  25.   
  26. public static char[] insertINDEX(char[] already, char a, int index) {   
  27.     char[] temp = new char[already.length + 1];   
  28.     for (int i = 0 ,j= 0; i < temp.length;j++, i++) {   
  29.         if (i == index) {   
  30.             temp[i] = a;   
  31.             j = j - 1;   
  32.         } else {   
  33.             temp[i] = already[j];   
  34.         }   
  35.     }   
  36.     return temp;   
  37. }   
  38.   
  39. public static void main(String [] args) throws IOException{   
  40.     System.out.println("Input A String!");   
  41.     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
  42.     String str = br.readLine();   
  43.     Set<String> set = new HashSet<String>();   
  44.     set = list(str);   
  45.     Iterator it = set.iterator();   
  46.     while(it.hasNext()){   
  47.         System.out.println(it.next());   
  48.     }   
  49. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值