问题描述
- java字符串组合排列递归问题
-
String a0,a1,a2,...,an;
a0="我饿全文";
a1="ABC";
a2="abc";
''''
an="wre";
每次从每个an中只取一个字符组合成字符串,全部排列出来,如:
我+A+a+w,我+A+a+r等。这个怎么弄啊?
解决方案
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String[] arr = new String[3];
arr[0] = "abc";
arr[1] = "123";
arr[2] = "xyz";
String[] arr1 = new String[1];
arr1[0] = "";
String[] arr2 = arr1;
for (int i = 0; i < arr.length; i++)
{
arr2 = new String[arr1.length * arr[i].length()];
for (int j = 0; j < arr2.length; j++)
{
arr2[j] = arr1[j / arr[i].length()] + String.valueOf(arr[i].charAt(j % arr[i].length()));
}
arr1 = arr2;
}
for (String s : arr2)
System.out.println(s);
}
}
解决方案二:
http://ideone.com/V0t44J
a1x
a1y
a1z
a2x
a2y
a2z
a3x
a3y
a3z
b1x
b1y
b1z
b2x
b2y
b2z
b3x
b3y
b3z
c1x
c1y
c1z
c2x
c2y
c2z
c3x
c3y
c3z
解决方案三:
反正我不喜欢递归,也没有必要递归。
解决方案四:
http://blog.chinaunix.net/uid-20690917-id-1568734.html
http://www.cnblogs.com/longhs/archive/2013/06/14/3135433.html
解决方案五:
谢谢,跟我要的还是有点不一样。
时间: 2024-11-05 06:17:09